Data Structures & AlgorithmsVery hardHCLTech · Wipro pattern
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 the tree (root = first preorder element, split inorder around it), what is its postorder traversal?
- A. 2, 5, 10, 30, 53, 60, 65, 72
- B. 53, 5, 2, 10, 30, 72, 65, 60
- C. 60, 65, 72, 30, 10, 2, 5, 53
- D. 2, 30, 10, 5, 60, 65, 72, 53
Correct answer: D. 2, 30, 10, 5, 60, 65, 72, 53
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, 30, 10, 5, 60, 65, 72, 53]. Simply reversing the preorder list, [60, 65, 72, 30, 10, 2, 5, 53], 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
- A directed graph has edges E→C, B→E, A→E, C→A, B→C, A→D, E→D. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a…
- A binary tree has preorder traversal [75, 38, 23, 7, 60, 94, 80, 95] and inorder traversal [7, 23, 38, 60, 75, 80, 94, 95]. Reconstructing…
- Undirected graph edges: D–A, D–B, A–C, B–C, A–B, D–C. Using a BFS 2-colouring, is this graph bipartite?
- Keys 18, 24, 4, 33, 84, 49, 74, 69, 8, 79 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…
- Undirected graph edges: E–B, A–D, A–C, A–B, B–D. Using a BFS 2-colouring, is this graph bipartite?
- A directed graph has edges C→A, B→D, D→B, D→C, D→A. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…