Data Structures & AlgorithmsVery hardIBM · Deloitte pattern

Keys 4, 49, 97, 15, 7, 96, 24, 50 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 15 and 97?

  1. A. 15
  2. B. 49
  3. C. 97
  4. D. 4

Correct answer: B. 49

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 15 and 97 in this tree, that gives 49. The root (4) 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