Data Structures Β· Visualization

Binary Tree: Lowest Common Ancestor

Binary Tree

The lowest common ancestor (LCA) of two nodes is the deepest node that has both as descendants (a node can be its own ancestor). Time: O(n) for a general binary tree (O(h) for a BST using ordering). Space: O(h) recursion stack.

Big-O: Traversals visit every node exactly once: O(n).

Binary Tree: Lowest Common Ancestor

Binary Tree

The lowest common ancestor (LCA) of two nodes is the deepest node that has both as descendants (a node can be its own ancestor). For a general binary tree, it's found by searching both subtrees and combining the results.

How Lowest Common Ancestor Works

  1. If the current node is null, or matches either target node, return the current node.
  2. Recursively search the left subtree for either target.
  3. Recursively search the right subtree for either target.
  4. If both the left and right searches return a non-null result, the current node is the LCA.
  5. Otherwise, propagate up whichever side returned a non-null result.

Lowest Common Ancestor Worked Example

Input: Find LCA of nodes 5 and 1 in tree rooted at 3, with children (5,1) under root, 5 has children (6,2)

  1. At root 3: search left (subtree rooted at 5) and right (subtree rooted at 1)
  2. Left search finds 5 itself (matches target) β†’ returns 5
  3. Right search finds 1 itself (matches target) β†’ returns 1
  4. Both sides non-null β†’ root 3 is the LCA

Output: LCA(5, 1) = 3

Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children β€” usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press β–Ά Run to watch it step by step.
Python Β· Binary Trees in Code
def inorder(n):
    if not n:
        return
    inorder(n.left)         # left
    print(n.val, end=" ")   # node
    inorder(n.right)        # right

# left -> root -> right
# (sorted order for a BST)
# example: lca
Input: Find LCA of nodes 5 and 1 in tree rooted at 3, with children (5,1) under root, 5 has children (6,2)

Behind the Scenes Execution

Press β–Ά Run in the visualizer β€” the call stack, registers and memory will replay here in sync with the code, line by line.

Lowest Common Ancestor Time & Space Complexity

TimeO(n) for a general binary tree (O(h) for a BST using ordering)
SpaceO(h) recursion stack

When to Use Lowest Common Ancestor

  • Finding relationship distance in family/org-chart trees
  • Network routing (common ancestor router)
  • Version control merge-base computation

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired β€” free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action β€” visualize the algorithms that operate on these structures, or take a guided course.