Data Structures & AlgorithmsVery hardIBM · Deloitte pattern
A binary tree has preorder traversal [11, 83, 45, 67, 61, 47, 95] and inorder traversal [11, 45, 47, 61, 67, 83, 95]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 95, 47, 61, 67, 45, 83, 11
- B. 11, 45, 47, 61, 67, 83, 95
- C. 11, 83, 45, 67, 61, 47, 95
- D. 47, 61, 67, 45, 95, 83, 11
Correct answer: D. 47, 61, 67, 45, 95, 83, 11
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 [47, 61, 67, 45, 95, 83, 11]. Simply reversing the preorder list, [95, 47, 61, 67, 45, 83, 11], 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
- Keys 41, 31, 21 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 38, 48, 58 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Undirected graph edges: B–D, C–A, E–B, E–A, D–E, C–B, A–B. Using a BFS 2-colouring, is this graph bipartite?
- A directed graph has edges E→A, C→D, D→B, C→E, D→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B]; B: [A, C, F]; C: [B, E, F]; D: [F]; E: [C]; F…
- A binary tree has preorder traversal [90, 24, 42, 38, 77, 67, 57, 44] and inorder traversal [24, 38, 42, 44, 57, 67, 77, 90]…