Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
A binary tree has preorder traversal [7, 75, 8, 32, 9, 18, 91, 86] and inorder traversal [7, 8, 9, 18, 32, 75, 86, 91]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 86, 91, 18, 9, 32, 8, 75, 7
- B. 18, 9, 32, 8, 86, 91, 75, 7
- C. 7, 75, 8, 32, 9, 18, 91, 86
- D. 7, 8, 9, 18, 32, 75, 86, 91
Correct answer: B. 18, 9, 32, 8, 86, 91, 75, 7
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 [18, 9, 32, 8, 86, 91, 75, 7]. Simply reversing the preorder list, [86, 91, 18, 9, 32, 8, 75, 7], 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 43, 53, 63 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A disjoint-set (union-find with union by rank and path compression) over 8 elements (0 to 7), each initially its own component, runs this…
- Undirected graph edges: D–C, B–C, A–C, A–B, A–D, B–D. Using a BFS 2-colouring, is this graph bipartite?
- Keys 64, 54, 44 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A binary tree has preorder traversal [23, 2, 94, 67, 57, 46, 81] and inorder traversal [2, 23, 46, 57, 67, 81, 94]. Reconstructing the…
- Build a binary min-heap in place from the array [29, 45, 8, 5, 74, 90, 69, 56, 38] using bottom-up heapify (sift-down from the last…