Data Structures & AlgorithmsHardTCS Digital · Infosys pattern
Keys 20, 8, 22, 4, 12, 10, 14, 25 are inserted into an empty BST. What is the lowest common ancestor of 10 and 14, and of 4 and 14?
- A. 12 and 8
- B. 8 and 8
- C. 12 and 20
- D. 10 and 4
Correct answer: A. 12 and 8
In a BST, walk down from the root until the two keys split. 10 and 14: both < 20 → go to 8; both > 8 → go to 12; 10 < 12 < 14 → LCA 12. 4 and 14: at 8, 4 < 8 < 14 → LCA 8. '20' is just the root, which is a common ancestor but not the lowest.
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
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 are inserted into an empty BST. A range query counts keys in [33, 62], pruning subtrees that…
- A robot moves only right or down from the top-left to the bottom-right cell of this 4 × 4 grid ('#' is blocked). How many paths are there?…
- DFS-based topological sort on the DAG with adjacency lists a: [b, c], b: [d], c: [d, e], d: [f], e: [f], f: [] starts at a, visits…
- DFS with white/grey/black colours detects a cycle in the directed graph with adjacency lists 1: [2, 3], 2: [4], 3: [4], 4: [5], 5: [3]…
- Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its…
- What is the size of a minimum vertex cover (a smallest set of vertices touching every edge) of the tree with edges 1–2, 1–3, 2–4, 2–5…