Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
A binary tree has preorder traversal [75, 38, 23, 7, 60, 94, 80, 95] and inorder traversal [7, 23, 38, 60, 75, 80, 94, 95]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 95, 80, 94, 60, 7, 23, 38, 75
- B. 75, 38, 23, 7, 60, 94, 80, 95
- C. 7, 23, 38, 60, 75, 80, 94, 95
- D. 7, 23, 60, 38, 80, 95, 94, 75
Correct answer: D. 7, 23, 60, 38, 80, 95, 94, 75
The first preorder element is the root; its position in the inorder list splits the remaining elements into left and right subtrees, applied recursively. Rebuilding the tree this way and walking it left-right-root gives postorder [7, 23, 60, 38, 80, 95, 94, 75]. Simply reversing the preorder list, [95, 80, 94, 60, 7, 23, 38, 75], only coincidentally matches postorder for some trees, not in general.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Undirected graph edges: D–A, D–B, A–C, B–C, A–B, D–C. Using a BFS 2-colouring, is this graph bipartite?
- Keys 18, 24, 4, 33, 84, 49, 74, 69, 8, 79 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…
- Undirected graph edges: E–B, A–D, A–C, A–B, B–D. Using a BFS 2-colouring, is this graph bipartite?
- A directed graph has edges C→A, B→D, D→B, D→C, D→A. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- A directed graph has edges A→B, C→D, D→C, C→B. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does this…
- Build a binary min-heap in place from the array [73, 61, 88, 9, 29, 24, 58, 42] using bottom-up heapify (sift-down from the last internal…