Coding interview problems โบ Trees & BSTs
๐ณ Trees & BSTs Medium
Lowest Common Ancestor of a BST
Lowest common ancestor of a BST explained: walk down until p and q split sides, an O(h) tested Python solution, common pitfalls and a live BST visual.
Also searched as: LCA in a binary search tree ยท lowest common ancestor BST iterative
Try it live Free
Binary Search Tree & Traversals
Insert values in the BST lab and trace the search paths to two values: the last node both paths share is their lowest common ancestor.
The problem
Given a binary search tree and two nodes p and q that exist in it, return their lowest common ancestor: the deepest node that has both p and q in its subtree (a node counts as its own descendant).
2 is an ancestor of 4, so it is the LCA.
The key insight
The BST ordering tells you where both nodes live without searching. If both values are smaller than the current node, the LCA is in the left subtree; if both are larger, it is in the right. The first node where they split (or where one of them equals the current node) is the answer.
Step-by-step approach
- Start at the root.
- If both
p.valandq.valare less thannode.val, move left. - If both are greater than
node.val, move right. - Otherwise they split here (or one equals this node): return the current node.
Python solution
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val, self.left, self.right = val, left, right
def lowest_common_ancestor_bst(root, p, q):
node = root
while node:
if p.val < node.val and q.val < node.val:
node = node.left
elif p.val > node.val and q.val > node.val:
node = node.right
else:
return node # p and q split here (or one is node)
return NoneComplexity
You walk a single root-to-node path, so time is the height h: O(log n) for a balanced BST. The iterative loop uses constant extra space.
Common mistakes
- Forgetting that a node can be its own ancestor, so when
pis an ancestor ofqthe answer isp. - Comparing node objects or the wrong field instead of values when deciding the direction.
- Using the general O(n) binary-tree solution and not mentioning that the BST property allows O(h).
Frequently asked questions
What is the time and space complexity of LCA in a BST?
O(h) time, following one path from the root, and O(1) space for the iterative version. For a balanced BST h is about log n.
What if p or q might not be in the tree?
The basic version assumes both exist. If not guaranteed, search for each value first, or confirm the returned node's subtree really contains both.
How is this different from LCA in a general binary tree?
Without ordering you cannot tell which side a value is on, so you must search both subtrees recursively, which costs O(n).
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Trees & BSTs Binary Search Tree & Traversals lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.