Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 D

A rooted tree has these parent→child edges: 3→7, 5→79, 89→3, 7→88, 12→87, 5→52, 52→8, 92→80, 80→12, 79→92, 5→20, 89→5, 80→76. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 8 and 3, i.e. the deepest node that is an ancestor of both?

  1. A. 52
  2. B. 3
  3. C. 89
  4. D. 8

Correct answer: C. 89

The LCA is the deepest common ancestor: lift the deeper node to the same depth, then climb both together (binary lifting does this in O(log n)). Answering the root, the parent of one node, or a node itself is the trap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic