Data Structures & AlgorithmsHardProduct-company MCQ pattern

Keys 5, 22, 60, 78, 66, 47, 79, 17, 35 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from the root, branching toward whichever side contains both values), what is the lowest common ancestor of 60 and 17?

  1. A. 5
  2. B. 17
  3. C. 60
  4. D. 22

Correct answer: D. 22

Starting at the root, move left while both values are smaller, move right while both are larger, and stop at the first node that splits the two (or equals one of them) — that node is the LCA. For 60 and 17 in this tree, that gives 22. The root (5) is only the LCA if the values fall on opposite sides of it, which may not hold here.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic