Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
A binary tree has inorder 4 8 2 5 1 6 3 7 and preorder 1 2 4 8 5 3 6 7. What is its postorder?
- A. 8 4 5 2 6 7 3 1
- B. 8 4 2 5 6 7 3 1
- C. 4 8 5 2 6 7 3 1
- D. 8 4 5 2 7 6 3 1
Correct answer: A. 8 4 5 2 6 7 3 1
Root 1 splits the inorder into 4 8 2 5 and 6 3 7. In the left part the root is 2; then 4 has 8 as its RIGHT child (8 comes after 4 in inorder). So postorder is 8 4 5 2, 6 7 3, 1. Making 8 a left child gives 4 8 … wrongly.
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
- 10, 4, 7, 1, 3 are inserted in that order into an empty binary min-heap (array form, sift-up after each insert). What is the array at the…
- The bottom-up build-heap procedure (sift down from the last internal node back to the root) turns [3, 1, 6, 5, 2, 4] into a max-heap. What…
- The maximum is deleted from the max-heap [50, 30, 40, 10, 20, 35]: the last element replaces the root and is sifted down. What is the heap…
- An undirected graph has edges A–B, A–C, B–D, C–D, C–E, D–F, E–F, and neighbours are always taken in alphabetical order. What is the DFS…
- An undirected graph has edges S–A, S–B, A–C, B–C, B–D, C–E, D–E, E–F. After BFS from S, which vertices are at level 2, and what is the…
- A directed graph has weighted edges A→B (4), A→C (1), C→B (2), B→D (1), C→D (5), D→E (3). Dijkstra's algorithm is run from A. What is the…