Data Structures & AlgorithmsHardGATE CS · ICPC regional · Product Company SDE
A binary tree has preorder traversal [52, 25, 42, 27, 64, 92, 78] and inorder traversal [25, 27, 42, 52, 64, 78, 92]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 25, 27, 42, 52, 64, 78, 92
- B. 27, 42, 25, 78, 92, 64, 52
- C. 52, 25, 42, 27, 64, 92, 78
- D. 78, 92, 64, 27, 42, 25, 52
Correct answer: B. 27, 42, 25, 78, 92, 64, 52
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 [27, 42, 25, 78, 92, 64, 52]. Simply reversing the preorder list, [78, 92, 64, 27, 42, 25, 52], 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 45, 55, 65 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 56, 48, 53, 36, 81, 29, 64 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…
- A binary tree has preorder traversal [72, 40, 18, 19, 54, 47, 92, 81] and inorder traversal [18, 19, 40, 47, 54, 72, 81, 92]…
- Build a binary min-heap in place from the array [37, 10, 58, 45, 19, 47, 86, 41] using bottom-up heapify (sift-down from the last internal…
- Build a binary min-heap in place from the array [34, 57, 50, 49, 69, 76, 15, 74] using bottom-up heapify (sift-down from the last internal…
- A disjoint-set (union-find with union by rank and path compression) over 9 elements (0 to 8), each initially its own component, runs this…