Data Structures & AlgorithmsVery hardProduct company OA
Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?
- A. 4
- B. 1
- C. 2
- D. 5
Correct answer: A. 4
Preorder + postorder cannot tell a lone LEFT child from a lone RIGHT child. Here B must be A's only child and C must be B's only child (any branching would put C before B in postorder). Each of the 2 single-child links can be left or right: 2 × 2 = 4 trees. Preorder + inorder would fix the tree uniquely.
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
- An alien language uses only the letters a, b, c, d. The words bca, bcd, ad, ac, dc are listed in sorted order. Building an edge from each…
- What is the maximum flow from S to T in the network with capacities S→A 10, S→B 5, A→B 15, A→T 6, B→T 10?
- Weighted undirected graph: A–B 1, B–C 2, C–A 2, C–D 1, B–D 2. How many DIFFERENT minimum spanning trees does it have?
- Keys 85, 62, 86, 26, 40, 54, 92, 17, 18, 67 are inserted into an empty AVL tree (giving preorder 62, 40, 18, 17, 26, 54, 86, 85, 67, 92)…
- Keys 20, 24, 27, 35, 38 are searched with frequencies 4, 1, 3, 6, 11 respectively (only successful searches). The cost of a BST is Σ…
- DFS on the directed graph with adjacency lists A: CE; B: EFG; C: B; D: AG; E: C; F: G; G: – starts at A (scanning lists in the order…