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?

  1. A. 12 and 8
  2. B. 8 and 8
  3. C. 12 and 20
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic