Before attempting this problem, you should be comfortable with:
Binary Trees - Understanding tree node structure and parent-child relationships
Depth First Search (DFS) - Recursive tree traversal to explore all nodes and combine subtree results
Breadth First Search (BFS) - Level-order traversal using a queue to build parent pointers
Hash Maps - Storing parent references for each node to trace paths back to the root
1. Depth First Search
Intuition
The lowest common ancestor (LCA) is the deepest node that has both p and q as descendants. We traverse the tree and track whether each subtree contains p, q, or both. The first node where we find both targets in its subtree (including itself) is the LCA. Once found, we can stop searching.
Algorithm
Define dfs(node) that returns a pair of booleans indicating whether p and q are found in the subtree rooted at node.
If node is null or LCA is already found, return [false, false].
Recursively search left and right subtrees.
Combine results: foundP = left[0] or right[0] or (node == p) and similarly for foundQ.
If both foundP and foundQ are true and LCA is not yet set, mark the current node as LCA.
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/publicclassSolution{privateTreeNode lca =null;publicTreeNodeLowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
lca =null;Dfs(root, p, q);return lca;}private(bool,bool)Dfs(TreeNode node,TreeNode p,TreeNode q){if(node ==null|| lca !=null){return(false,false);}var left =Dfs(node.left, p, q);var right =Dfs(node.right, p, q);bool foundP = left.Item1 || right.Item1 || node == p;bool foundQ = left.Item2 || right.Item2 || node == q;if(foundP && foundQ && lca ==null){
lca = node;}return(foundP, foundQ);}}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funclowestCommonAncestor(root, p, q *TreeNode)*TreeNode {var lca *TreeNode
var dfs func(node *TreeNode)(bool,bool)
dfs =func(node *TreeNode)(bool,bool){if node ==nil|| lca !=nil{returnfalse,false}
leftP, leftQ :=dfs(node.Left)
rightP, rightQ :=dfs(node.Right)
foundP := leftP || rightP || node == p
foundQ := leftQ || rightQ || node == q
if foundP && foundQ && lca ==nil{
lca = node
}return foundP, foundQ
}dfs(root)return lca
}
/**
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/class Solution {privatevar lca: TreeNode?=nullfunlowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode?{
lca =nulldfs(root, p, q)return lca
}privatefundfs(node: TreeNode?, p: TreeNode?, q: TreeNode?): Pair<Boolean, Boolean>{if(node ==null|| lca !=null){returnPair(false,false)}val left =dfs(node.left, p, q)val right =dfs(node.right, p, q)val foundP = left.first || right.first || node === p
val foundQ = left.second || right.second || node === q
if(foundP && foundQ && lca ==null){
lca = node
}returnPair(foundP, foundQ)}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/classSolution{privatevar lca:TreeNode?=nilfunclowestCommonAncestor(_ root:TreeNode?,_ p:TreeNode?,_ q:TreeNode?)->TreeNode?{
lca =nildfs(root, p, q)return lca
}privatefuncdfs(_ node:TreeNode?,_ p:TreeNode?,_ q:TreeNode?)->(Bool,Bool){guardlet node = node, lca ==nilelse{return(false,false)}letleft=dfs(node.left, p, q)letright=dfs(node.right, p, q)let foundP =left.0||right.0|| node === p
let foundQ =left.1||right.1|| node === q
if foundP && foundQ && lca ==nil{
lca = node
}return(foundP, foundQ)}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
2. Depth First Search (Optimal)
Intuition
We can simplify the approach by returning the node itself rather than boolean flags. If a node is p or q, we return it immediately. Otherwise, we recursively search both subtrees. If both return non-null values, the current node must be the LCA. If only one side returns a value, we propagate that up since both targets are in that subtree.
Algorithm
If root is null, return null.
If root equals p or q, return root.
Recursively call on the left and right children.
If both left and right return non-null, return root (it's the LCA).
Otherwise, return whichever side is non-null (or null if both are null).
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:deflowestCommonAncestor(
self, root:'TreeNode', p:'TreeNode', q:'TreeNode')->'TreeNode':if root isNoneor root is p or root is q:return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)if left and right:return root
return left if left else right
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/publicclassSolution{publicTreeNodelowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){if(root ==null|| root == p || root == q){return root;}TreeNode left =lowestCommonAncestor(root.left, p, q);TreeNode right =lowestCommonAncestor(root.right, p, q);if(left !=null&& right !=null){return root;}return left !=null? left : right;}}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/classSolution{public:
TreeNode*lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){if(!root || root == p || root == q){return root;}
TreeNode* left =lowestCommonAncestor(root->left, p, q);
TreeNode* right =lowestCommonAncestor(root->right, p, q);if(left && right){return root;}return left ? left : right;}};
/**
* // Definition for a Node.
* function Node(val) {
* this.val = val;
* this.left = null;
* this.right = null;
* this.parent = null;
* }
*/classSolution{/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/lowestCommonAncestor(root, p, q){if(!root || root === p || root === q){return root;}const left =this.lowestCommonAncestor(root.left, p, q);const right =this.lowestCommonAncestor(root.right, p, q);return left && right ? root :(left || right);}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/publicclassSolution{publicTreeNodeLowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){if(root ==null|| root == p || root == q){return root;}TreeNode left =LowestCommonAncestor(root.left, p, q);TreeNode right =LowestCommonAncestor(root.right, p, q);if(left !=null&& right !=null){return root;}return left ?? right;}}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funclowestCommonAncestor(root, p, q *TreeNode)*TreeNode {if root ==nil|| root == p || root == q {return root
}
left :=lowestCommonAncestor(root.Left, p, q)
right :=lowestCommonAncestor(root.Right, p, q)if left !=nil&& right !=nil{return root
}if left !=nil{return left
}return right
}
/**
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/class Solution {funlowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode?{if(root ==null|| root === p || root === q){return root
}val left =lowestCommonAncestor(root.left, p, q)val right =lowestCommonAncestor(root.right, p, q)if(left !=null&& right !=null){return root
}return left ?: right
}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/classSolution{funclowestCommonAncestor(_ root:TreeNode?,_ p:TreeNode?,_ q:TreeNode?)->TreeNode?{if root ==nil|| root === p || root === q {return root
}letleft=lowestCommonAncestor(root?.left, p, q)letright=lowestCommonAncestor(root?.right, p, q)ifleft!=nil&&right!=nil{return root
}returnleft??right}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
3. Breadth First Search
Intuition
Instead of recursion, we can use BFS to build a parent pointer map for each node. Once we have parent pointers, we trace the path from p to the root and store all ancestors. Then we trace from q upward until we hit a node that's already in p's ancestor set. That node is the LCA.
Algorithm
Use BFS to traverse the tree, storing each node's parent in a hash map.
Continue BFS until both p and q are found in the parent map.
Create an ancestors set and trace from p to the root, adding each node to the set.
Trace from q upward until finding a node that exists in the ancestors set.
/**
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/class Solution {funlowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode?{if(root ==null)returnnullval parent = HashMap<TreeNode, TreeNode?>()val queue: java.util.Queue<TreeNode>= java.util.LinkedList()
parent[root]=null
queue.add(root)var pNode = p
var qNode = q
while(!parent.containsKey(pNode)||!parent.containsKey(qNode)){val node = queue.poll()
node.left?.let{
parent[it]= node
queue.add(it)}
node.right?.let{
parent[it]= node
queue.add(it)}}val ancestors = HashSet<TreeNode>()while(pNode !=null){
ancestors.add(pNode)
pNode = parent[pNode]}while(qNode !in ancestors){
qNode = parent[qNode]}return qNode
}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/classSolution{funclowestCommonAncestor(_ root:TreeNode?,_ p:TreeNode?,_ q:TreeNode?)->TreeNode?{guardlet root = root else{returnnil}var parent:[ObjectIdentifier:TreeNode?]=[ObjectIdentifier(root):nil]var queue:[TreeNode]=[root]var pNode = p
var qNode = q
while(pNode ==nil|| parent[ObjectIdentifier(pNode!)]==nil&& pNode !== root)||(qNode ==nil|| parent[ObjectIdentifier(qNode!)]==nil&& qNode !== root){let node = queue.removeFirst()ifletleft= node.left{
parent[ObjectIdentifier(left)]= node
queue.append(left)}ifletright= node.right{
parent[ObjectIdentifier(right)]= node
queue.append(right)}}var ancestors =Set<ObjectIdentifier>()whilelet pn = pNode {
ancestors.insert(ObjectIdentifier(pn))
pNode = parent[ObjectIdentifier(pn)]??nil}whilelet qn = qNode,!ancestors.contains(ObjectIdentifier(qn)){
qNode = parent[ObjectIdentifier(qn)]??nil}return qNode
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
Common Pitfalls
Returning Early Without Checking Both Subtrees
A common mistake in the recursive approach is returning as soon as you find p or q, without considering that the other target might be in a different subtree. The correct approach returns the current node if it matches a target, but the parent call must still check both subtree results to determine if the current node is the LCA.
Confusing Node Reference with Node Value
The problem asks for the LCA of specific nodes p and q, not nodes with values p.val and q.val. Since tree nodes can have duplicate values, you must compare node references (node == p) rather than values (node.val == p.val). Using value comparison fails when multiple nodes share the same value.
Mishandling the Case Where One Node is Ancestor of the Other
When p is an ancestor of q (or vice versa), the LCA is the ancestor node itself. A common bug is failing to recognize this case and continuing to search unnecessarily. In the optimal recursive solution, returning the node immediately when it matches p or q naturally handles this case, since the other target will be found in that node's subtree.