Skip to main content

Lowest Common Ancestor

Learn Lowest Common Ancestor (Trees, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is LCA? The Lowest Common Ancestor of two nodes p and q is the DEEPEST node that has BOTH p and q as descendants (a node is considered a descendant of itself). For a general binary tree, find it with a bottom-up recursive search: a node IS the LCA if p and q are found in DIFFERENT subtrees (or the node itself is p or q).

More lessons in Trees