Skip to content

6.3 Find the lowest common ancestor of two nodes

You find the lowest common ancestor of two nodes: the deepest node that is an ancestor of both.

You have two nodes in a hierarchy and want the first node they share on the way up to the top. Two nodes in the same tree have the root as a common ancestor, but you want the deepest one. It is called the lowest common ancestor. It is the last node that the paths from the root to the two nodes have in common. You need this to find:

  • the innermost element of a syntax tree that encloses two expressions;
  • the smallest folder that contains two files;
  • the lowest manager who is responsible for two employees;
  • the most recent common ancestor of two people in a family tree that records one parent per person.

The example graph family models a family tree that records one parent per person. It has an edge from each parent to each child. What is the lowest common ancestor of the second cousins Greta and Ida? What is it for Greta and her uncle Emil, for the siblings Hugo and Greta, and for Dora and her son Hugo?

The family tree and the people asked about
The lowest common ancestors of the four pairs
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
0: Ada1: Ben2: Cleo3: Dora4: Emil5: Finn6: Greta7: Hugo8: Ida9: Jonas
Input
  • Person asked about
Output
  • Lowest common ancestor

The picture after shows the results of all four questions: Ada, Ben and Dora. Dora is also one of the people asked about. Ada is the lowest common ancestor of Greta and Ida: the paths from the root to the two of them have only Ada in common.

Ancestry ancestry = Ancestry.of(family);
int commonOfSecondCousins = ancestry.lowestCommonAncestorOf(greta, ida); // 0 - Ada
int commonOfUncleAndNiece = ancestry.lowestCommonAncestorOf(greta, emil); // 1 - Ben
int commonOfSiblings = ancestry.lowestCommonAncestorOf(hugo, greta); // 3 - Dora
// 3 - Dora herself
int commonOfMotherAndSon = ancestry.lowestCommonAncestorOf(dora, hugo);
Full example
let ancestry = Ancestry::of(&family)?;
// Some(0) - Ada
let common_of_second_cousins = ancestry.lowest_common_ancestor_of(greta, ida);
// Some(1) - Ben
let common_of_uncle_and_niece = ancestry.lowest_common_ancestor_of(greta, emil);
// Some(3) - Dora
let common_of_siblings = ancestry.lowest_common_ancestor_of(hugo, greta);
// Some(3) - Dora herself
let common_of_mother_and_son = ancestry.lowest_common_ancestor_of(dora, hugo);
Full example
const ancestry = Ancestry.of(family);
// 0 - Ada
const commonOfSecondCousins = ancestry.lowestCommonAncestorOf(greta, ida);
// 1 - Ben
const commonOfUncleAndNiece = ancestry.lowestCommonAncestorOf(greta, emil);
// 3 - Dora
const commonOfSiblings = ancestry.lowestCommonAncestorOf(hugo, greta);
// 3 - Dora herself
const commonOfMotherAndSon = ancestry.lowestCommonAncestorOf(dora, hugo);
Full example
Ancestry ancestry = Ancestry.Of(family);
// 0 - Ada
int commonOfSecondCousins = ancestry.LowestCommonAncestorOf(greta, ida);
// 1 - Ben
int commonOfUncleAndNiece = ancestry.LowestCommonAncestorOf(greta, emil);
// 3 - Dora
int commonOfSiblings = ancestry.LowestCommonAncestorOf(hugo, greta);
// 3 - Dora herself
int commonOfMotherAndSon = ancestry.LowestCommonAncestorOf(dora, hugo);
Full example

Ada is the lowest common ancestor of Greta and Ida. Ben is that of Greta and her uncle Emil, and Dora is that of the siblings Hugo and Greta. A node is its own ancestor, so the lowest common ancestor of Dora and her son Hugo is Dora herself.