Data Structures & AlgorithmsHardGATE CS · Advanced
Every node of a binary tree has either 0 or 2 children. Its preorder traversal is W S M Q R P K Z T and its postorder traversal is Q R M K Z P S T W. What is its inorder traversal?
- A. T W Q M R S K P Z
- B. T W Z P K S R M Q
- C. Q M R S K P Z W T
- D. R M Q S K P Z W T
Correct answer: C. Q M R S K P Z W T
For a full binary tree the pair (preorder, postorder) is unique: the node after the root in preorder is the left child, and its position in the postorder gives the size of the left subtree. Rebuild recursively, then read inorder.
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 graph has the vertices A–G and the edges A–B, A–C, A–D, B–D, B–F, B–G, C–D, C–F, C–G, D–F, E–F. In how many ways can the vertices be…
- Floyd-Warshall is run on a directed graph with vertices 1-6 and arcs (weights): 1→2 (9), 1→3 (8), 1→4 (8), 1→5 (1), 2→5 (3), 2→6 (8), 3→1…
- A flow network has source S = A and sink T = G (vertices A–G) with arcs and capacities: A→D (1), A→E (6), A→G (6), B→C (2), B→E (5), C→F…
- How many structurally different binary search trees can be built on the keys 1, 2, ..., 8 (all distinct shapes, one per key set) whose…
- A rooted tree has the vertices A–M with root A, given by parents: A is the parent of B, B is the parent of C, B is the parent of D, B is…
- A weighted undirected graph on A–H has the edges (weights): A–D (16), A–E (22), A–F (26), A–G (5), A–H (4), B–C (9), B–H (23), C–D (13)…