Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
A binary tree has preorder traversal [21, 13, 54, 28, 40, 34] and inorder traversal [13, 21, 28, 34, 40, 54]. Reconstructing the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 13, 34, 40, 28, 54, 21
- B. 13, 21, 28, 34, 40, 54
- C. 34, 40, 28, 54, 13, 21
- D. 21, 13, 54, 28, 40, 34
Correct answer: A. 13, 34, 40, 28, 54, 21
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 [13, 34, 40, 28, 54, 21]. Simply reversing the preorder list, [34, 40, 28, 54, 13, 21], 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
- What are the time complexities of P and Q? c int P(int n) { if (n <= 1) return 1; return P(n/2) + P(n/2); } int Q(int n) { if (n <= 1)…
- Keys 30, 11, 59, 70, 43, 35 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain) into a table of…
- Undirected graph edges: A–C, A–D, D–C, C–B. Using a BFS 2-colouring, is this graph bipartite?
- 0/1 knapsack, capacity 11. Items (weight, value): (4, 6), (1, 7), (6, 3), (2, 2). What is the maximum total value?
- Keys 92, 78, 40, 55, 53, 90, 42 are inserted via separate chaining (h(k) = k mod 5, new keys appended to the end of the chain) into a…
- A binary tree has preorder traversal [53, 5, 2, 10, 30, 72, 65, 60] and inorder traversal [2, 5, 10, 30, 53, 60, 65, 72]. Reconstructing…