Trees & BST · Pattern 7 of 12
Lowest common ancestor: in a binary tree and in a BST
Ask both subtrees whether they contain p or q. The first node where the answers come from different sides (or the node is one of them) is the lowest common ancestor.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- The deepest node that has both p and q below it
- A path between two nodes (it goes up to their LCA and down again)
- Nodes with parent pointers, or a "region" hierarchy given as lists
The template
TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) return root; // found one, or nothing
TreeNode l = lowestCommonAncestor(root.left, p, q);
TreeNode r = lowestCommonAncestor(root.right, p, q);
if (l != null && r != null) return root; // one on each side: this is it
return l != null ? l : r; // pass up whatever was found
}
// in a BST the values tell you where p and q are: walk down until they split
TreeNode lowestCommonAncestorBST(TreeNode root, TreeNode p, TreeNode q) {
while (root != null) {
if (p.val < root.val && q.val < root.val) root = root.left;
else if (p.val > root.val && q.val > root.val) root = root.right;
else return root;
}
return null;
} def lowest_common_ancestor(root, p, q):
if not root or root is p or root is q:
return root # found one, or nothing
l = lowest_common_ancestor(root.left, p, q)
r = lowest_common_ancestor(root.right, p, q)
if l and r:
return root # one on each side: this is it
return l or r # pass up whatever was found
# in a BST the values tell you where p and q are: walk down until they split
def lowest_common_ancestor_bst(root, p, q):
while root:
if p.val < root.val and q.val < root.val:
root = root.left
elif p.val > root.val and q.val > root.val:
root = root.right
else:
return root
return None TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root == p || root == q) return root; // found one, or nothing
TreeNode* l = lowestCommonAncestor(root->left, p, q);
TreeNode* r = lowestCommonAncestor(root->right, p, q);
if (l && r) return root; // one on each side: this is it
return l ? l : r; // pass up whatever was found
}
// in a BST the values tell you where p and q are: walk down until they split
TreeNode* lowestCommonAncestorBST(TreeNode* root, TreeNode* p, TreeNode* q) {
while (root) {
if (p->val < root->val && q->val < root->val) root = root->left;
else if (p->val > root->val && q->val > root->val) root = root->right;
else return root;
}
return nullptr;
} function lowestCommonAncestor(root, p, q) {
if (!root || root === p || root === q) return root; // found one, or nothing
const l = lowestCommonAncestor(root.left, p, q);
const r = lowestCommonAncestor(root.right, p, q);
if (l && r) return root; // one on each side: this is it
return l || r; // pass up whatever was found
}
// in a BST the values tell you where p and q are: walk down until they split
function lowestCommonAncestorBST(root, p, q) {
while (root) {
if (p.val < root.val && q.val < root.val) root = root.left;
else if (p.val > root.val && q.val > root.val) root = root.right;
else return root;
}
return null;
} The variations
-
1Recursive LCA in a binary tree
Return the node if it is p or q, otherwise return what the children found. With many target nodes, return the node when it is in the set. For directions, find the LCA, then the path from it to each node.
-
2LCA in a BST
If both values are smaller, go left; if both are bigger, go right; otherwise they split here. O(h) and no recursion needed.
-
3With parent pointers
Walk up from both nodes. Like the intersection of two linked lists: when one pointer reaches the top, restart it at the other node, and they meet at the LCA. Regions are the same with a parent map.
Now you solve
Mistakes to avoid
- The basic version assumes p and q are both in the tree. If one may be missing, count how many you actually found before trusting the answer.
- Comparing values instead of nodes in a plain binary tree: values can repeat.
- Directions between two nodes: the part from the start up to the LCA is all
Umoves, whatever the turns were.
Mark it done once you can write the template from memory and you have solved the worked examples yourself.