Problem Statement:
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Examples:
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1
Output: 2
Constraints:
- The number of nodes in the tree is in the range
[2, 105]. -109 <= Node.val <= 109- All
Node.valare unique. p != qpandqwill exist in the BST.
Approach
- If both
pandqare less than root, LCA lies in theleft subtree → recurse left. - If both
pandqare greater than root, LCA lies in theright subtree → recurse right. - Otherwise, split happens at root (one node on each side or one is the root itself) →
this root is the LCA.
Time Complexity:
Time Complexity = O(n)
Space Complexity:
Space Complexity = O(h)(h=tree height)
Dry Run
root = Node(6)
├── left: Node(2)
│ ├── left: Node(0)
│ └── right: Node(4)
│ ├── left: Node(3)
│ └── right: Node(5)
└── right: Node(8)
├── left: Node(7)
└── right: Node(9)
Initial State:
→ root = Node(6)
→ p = Node(2)
→ q = Node(8)
Step 1:
→ p.val = 2, q.val = 8
→ Since p.val < root.val and q.val > root.val → Nodes lie on different sides
→ Return current root → Node(6) is the Lowest Common Ancestor
Final Result:
LCA = Node(6) → The lowest common ancestor of Node(2) and Node(8)
var lowestCommonAncestor = function(root, p, q) {
if(p.val < root.val && q.val < root.val){
return lowestCommonAncestor(root.left, p, q);
} else if(p.val > root.val && q.val > root.val){
return lowestCommonAncestor(root.right, p,q);
} else {
return root;
}
};
def lowestCommonAncestor(root, p, q):
if p.val < root.val and q.val < root.val:
return lowestCommonAncestor(root.left, p, q)
elif p.val > root.val and q.val > root.val:
return lowestCommonAncestor(root.right, p, q)
else:
return root
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
} else if(p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val < root->val && q->val < root->val) {
return lowestCommonAncestor(root->left, p, q);
} else if(p->val > root->val && q->val > root->val) {
return lowestCommonAncestor(root->right, p, q);
} else {
return root;
}
}
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if (p->val < root->val && q->val < root->val) {
return lowestCommonAncestor(root->left, p, q);
} else if (p->val > root->val && q->val > root->val) {
return lowestCommonAncestor(root->right, p, q);
} else {
return root;
}
}
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val < root.val && q.val < root.val) {
return LowestCommonAncestor(root.left, p, q);
} else if (p.val > root.val && q.val > root.val) {
return LowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
