Data Structures & AlgorithmsVery hardProduct company OA
Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?
- A. [20, 10, 30, 5, 25, 22, 35]
- B. [20, 10, 30, 5, 15, 18, 35]
- C. [20, 10, 30, 12, 15, 22, 35]
- D. [20, 10, 30, 5, 15, 22, 35]
Correct answer: D. [20, 10, 30, 5, 15, 22, 35]
Every key must respect ALL its ancestors, not just its parent. [.., 25, ..]: 25 > 10 is fine locally but sits in 20's LEFT subtree. [.., 18, ..]: 18 < 30 is fine locally but sits in 20's RIGHT subtree. [.., 12, ..]: 12 is 10's LEFT child but larger than 10. Only [20, 10, 30, 5, 15, 22, 35] passes the (low, high) bound check at every node.
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
- 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?
- 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 Σ…