Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A rooted binary tree stored as a parent array over nodes 0–9: parent of node i is [-1, 0, 0, 1, 1, 2, 3, 3, 4, 5][i] (node 0 is the root). What is the lowest common ancestor of nodes 8 and 7?

  1. A. 2
  2. B. 0
  3. C. 1
  4. D. 3

Correct answer: C. 1

List the ancestors of one node, then walk up from the other until hitting that set. Reporting the parent of the first node instead of the first shared ancestor is a common slip.

More Coding Interview Problems questions

Exams that ask this topic