Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
A binary tree has preorder traversal [90, 31, 36, 77, 58, 43, 83] and inorder traversal [31, 36, 43, 58, 77, 83, 90]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 90, 31, 36, 77, 58, 43, 83
- B. 31, 36, 43, 58, 77, 83, 90
- C. 83, 43, 58, 77, 36, 31, 90
- D. 43, 58, 83, 77, 36, 31, 90
Correct answer: D. 43, 58, 83, 77, 36, 31, 90
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 [43, 58, 83, 77, 36, 31, 90]. Simply reversing the preorder list, [83, 43, 58, 77, 36, 31, 90], 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: A–D, D–C, B–C, C–A. Using a BFS 2-colouring, is this graph bipartite?
- Keys 57, 11, 34, 33, 80, 9, 72, 24, 56, 66 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain)…
- Undirected graph edges: B–C, C–A, E–C, E–D, E–B. Using a BFS 2-colouring, is this graph bipartite?
- Build a binary min-heap in place from the array [4, 37, 81, 88, 64, 1, 84, 86] using bottom-up heapify (sift-down from the last internal…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [D, F]; B: [C, F]; C: [B, E]; D: [A, E]; E: [C, D]…
- Keys 93, 67, 60, 15, 3, 58, 52, 13, 92 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into…