Lowest Common Ancestor of a Binary Search Tree

Difficulty: Medium

You're given the root of a binary search tree (at every node, the left subtree is smaller and the right subtree is larger) and two nodes p and q that exist in the tree.

Find their lowest common ancestor: the deepest node that has both p and q as descendants. A node counts as its own descendant, so if p is an ancestor of q, the answer is p.

Examples

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6

2 lives in the left subtree of 6 and 8 lives in the right subtree, so 6 is the deepest node that has both underneath it.

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2

4 is a descendant of 2, and a node is allowed to be its own ancestor, so 2 is the answer.

Constraints

  • The number of nodes is in the range [2, 10^5].

  • All node values are unique.

  • p and q both exist in the tree and p != q.

Approach

A solution for any binary tree searches both subtrees to see where p and q turn up. That works here too, but ignores that this tree is sorted.

In a BST, comparing a value to the current node tells you which subtree it's in, with no searching. Walking down from the root, the first node where p and q go different ways is the split point: the lowest common ancestor.

Solutions

General Tree Approach — Recursive Search

This ignores the BST property and treats the tree as any binary tree. It's always correct, but not the fastest option here.

At each node, search the left and right subtrees for p and q. If the node is p or q, or both children found something, this node is the split point, so return it. Otherwise pass up whichever side found something.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function lowestCommonAncestor(root, p, q) {
  if (root === null || root === p || root === q) return root;

  const left = lowestCommonAncestor(root.left, p, q);
  const right = lowestCommonAncestor(root.right, p, q);

  if (left !== null && right !== null) return root;
  return left !== null ? left : right;
}

Time: O(n) · Space: O(h) — recursion stack, where h is the tree's height

Optimal — Use BST Ordering

A BST tells you where a value belongs from comparisons alone, so you don't need to search both subtrees.

Start at the root. If both p.val and q.val are smaller than the node's value, move left. If both are larger, move right. As soon as neither is true (they split, or one equals the node), you're at the lowest common ancestor.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function lowestCommonAncestor(root, p, q) {
  let node = root;

  while (node !== null) {
    if (p.val < node.val && q.val < node.val) {
      node = node.left;
    } else if (p.val > node.val && q.val > node.val) {
      node = node.right;
    } else {
      return node;
    }
  }

  return null;
}

Time: O(h) — one pass down the tree, where h is its height · Space: O(1) — iterative, no recursion stack