Lowest Common Ancestor of a Binary Search Tree
Medium · Trees
Jump to codeIn a BST with distinct values, find the lowest node whose subtree contains two given nodes p and q. A node may be its own ancestor; both targets exist.
Examples
Example 1
root = [6,2,8,0,4,7,9,null,null,3,5] p = 2 q = 8
Output: 6
Example 2
root = [6,2,8,0,4,7,9,null,null,3,5] p = 2 q = 4
Output: 2
Constraints
- 2 <= The number of nodes in the tree <= 100
- -100 <= Node.val <= 100
- p != q
- p and q will both exist in the BST
Function contract
Trees use level-order arrays, with null for a missing child. Your code receives TreeNode objects with val, left, and right fields.
p and q refer to the existing nodes in root with those values.
Need a hint?
Use the ordering rule before exploring any children.
Asked in real interviews
Candidates reported versions of Lowest Common Ancestor of a Binary Search Tree from these company interviews. Some company questions are locked and open with Premium or XP.
- Lowest Common Ancestor in a Binary Search TreeAmazon
- Find the Lowest Common Ancestor in a Binary Search TreeMicrosoft
Solution approach
If both target values are smaller than the current node, move left. If both are larger, move right. Otherwise the targets split here, or the current node equals a target, so return it. No full traversal is needed.
Complexity
O(h) time and O(1) extra space with iteration.
Python reference solution
def lowestCommonAncestor(root, p, q):
low, high = min(p.val, q.val), max(p.val, q.val)
while root:
if root.val < low:
root = root.right
elif root.val > high:
root = root.left
else:
return root
JavaScript solution
function lowestCommonAncestor(root, p, q) {
const low = Math.min(p.val, q.val), high = Math.max(p.val, q.val);
while (root) {
if (root.val < low) root = root.right;
else if (root.val > high) root = root.left;
else return root;
}
return null;
}
Java solution
import java.util.*;
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int low = Math.min(p.val, q.val), high = Math.max(p.val, q.val);
while (root != null) {
if (root.val < low) root = root.right;
else if (root.val > high) root = root.left;
else return root;
}
return null;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int low = min(p->val, q->val), high = max(p->val, q->val);
while (root) {
if (root->val < low) root = root->right;
else if (root->val > high) root = root->left;
else return root;
}
return nullptr;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
6,
2,
8,
0,
4,
7,
9,
null,
null,
3,
5
],
2,
8
]6
[
[
6,
2,
8,
0,
4,
7,
9,
null,
null,
3,
5
],
2,
4
]2
[
[
6,
2,
8,
0,
4,
7,
9,
null,
null,
3,
5
],
3,
5
]4
[
[
6,
2,
8,
0,
4,
7,
9,
null,
null,
3,
5
],
7,
9
]8
[
[
6,
2,
8,
0,
4,
7,
9,
null,
null,
3,
5
],
6,
8
]6
Drafts are saved per language in this browser when storage is available.