Data Structures & AlgorithmsVery hardGATE CS · ICPC regional · Product Company SDE
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 tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 2, 23, 46, 57, 67, 81, 94
- B. 23, 2, 94, 67, 57, 46, 81
- C. 2, 46, 57, 81, 67, 94, 23
- D. 81, 46, 57, 67, 94, 2, 23
Correct answer: C. 2, 46, 57, 81, 67, 94, 23
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 [2, 46, 57, 81, 67, 94, 23]. Simply reversing the preorder list, [81, 46, 57, 67, 94, 2, 23], 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
- 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…
- Keys 45, 92, 18, 65, 91, 86, 78, 95 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges…
- Keys 55, 50, 19, 76, 39, 98, 34, 63, 58, 69 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…
- A trie is built from the words {cat, card, den, cats, dog, do, cap}. Using the trie (walk the prefix's characters, then count all words in…
- Build a binary min-heap in place from the array [2, 63, 36, 74, 23, 20, 15, 22] using bottom-up heapify (sift-down from the last internal…
- Build a binary min-heap in place from the array [47, 72, 84, 82, 36, 61, 51, 7, 2] using bottom-up heapify (sift-down from the last…