Data Structures & AlgorithmsHardHCLTech · Wipro pattern
A binary tree has preorder traversal [9, 97, 20, 59, 28, 78] and inorder traversal [9, 20, 28, 59, 78, 97]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 9, 20, 28, 59, 78, 97
- B. 28, 78, 59, 20, 97, 9
- C. 9, 97, 20, 59, 28, 78
- D. 78, 28, 59, 20, 97, 9
Correct answer: B. 28, 78, 59, 20, 97, 9
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 [28, 78, 59, 20, 97, 9]. Simply reversing the preorder list, [78, 28, 59, 20, 97, 9], 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 55, 72, 25, 37, 27, 49, 91 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…
- An undirected graph has edges (u–v: weight) A–B:4, B–D:7, A–D:15, A–C:29, B–C:28, C–D:29. What is the weight of its minimum spanning tree?
- Keys 27, 96, 68, 47, 51, 92, 12 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges on…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [G]; B: [C]; C: [B, D]; D: [C, F, G]; E: [F]; F: [D…
- Keys 35, 45, 55 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 60, 70, 80 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…