Skip to content
Problem 236 · LeetCode 236(opens in a new tab)

Lowest Common Ancestor of a Binary Tree

MediumDepth-first search

You get the root of a binary tree and two different nodes in it, p and q. The tree has no ordering: any value can sit anywhere.

Return their lowest common ancestor: the deepest node that has both p and q in its subtree. A node is part of its own subtree, so when p lies under q, the answer is q itself.

Example 1

Input:  root = [8, 3, 6, 1, 4, null, 9], p = 4, q = 9
Output: 8
Why:    4 is under 3 and 9 is under 6; they only meet at the root
2 more examples and the constraints

Example 2

Input:  root = [8, 3, 6, 1, 4, null, 9], p = 3, q = 4
Output: 3
Why:    4 is a child of 3, and 3 counts as its own ancestor

Example 3

Input:  root = [2, 7], p = 7, q = 2
Output: 2
Why:    7 hangs under the root, 2

Constraints

  • 2 ≤ number of nodes ≤ 100,000
  • −1,000,000,000 ≤ node value ≤ 1,000,000,000, and all values are distinct
  • p and q are different nodes, and both are in the tree

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Parent links and an ancestor setO(n)
  2. One recursive searchO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a tree of 100,000 nodesVerdict
Parent links and an ancestor setO(n)
Works; O(n) extra memory
One recursive searchO(n)
Best; one pass, no map

n = number of nodes, h = height of the tree (about 17 when balanced, up to n for a chain).

Edge cases to test

  • One node is under the other. p = 3, q = 4 in Example 1 gives 3: the call on 3 returns at once, and nothing else finds q.
  • The answer is the root. p and q in different halves, like 4 and 9: the root is the first node where both sides answer.
  • A chain-shaped tree. The height can reach 100,000. A recursion that deep can overflow; the parent-map version has no recursion.
  • p or q is the root. The very first call returns the root, and that is the answer: every other node is below it.

Common mistakes

  • Searching for p and q by value with a BST-style comparison; this tree has no order.
  • Continuing to search below p after finding it, which makes the code more complex for no gain.
  • Returning left whenever it is set, even when right also found a node.

How to talk through it

  1. 1Clarify. Are both nodes always in the tree? Can one be the ancestor of the other? Is the tree ordered?
  2. 2Start simple. Record parents, collect p's ancestors in a set, then climb from q to the first shared node. O(n) time and space.
  3. 3Find the recursion. Each subtree reports whether it holds p or q. The first node with reports from both sides is the answer.
  4. 4Code it. Return the node on null, p or q; recurse left and right; return root if both are set, else the one that is.
  5. 5Test out loud. Try p and q in different halves, and p sitting directly above q.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?