235. Lowest Common Ancestor of a Binary Search Tree
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Trees
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a BST and two nodes p and q guaranteed to exist in it, find the deepest node in the tree that is an ancestor of both p and q (a node is considered its own ancestor).
Approach
- Exploit the BST ordering property: at any node, all left descendants are smaller and all right descendants are larger.
- Walk down from the root: if both p and q are smaller than the current node, the LCA must be in the left subtree; if both are larger, go right.
- The first node where the two targets "split" (one is smaller or equal, the other is larger or equal) is by definition their lowest common ancestor — no deeper node can claim both.
- An iterative traversal suffices, keeping space constant.
#include <algorithm>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// Ensure lo <= hi so we only need one comparison each direction.
int lo = min(p->val, q->val);
int hi = max(p->val, q->val);
while (root) {
if (root->val < lo) {
// Both targets are in the right subtree.
root = root->right;
} else if (root->val > hi) {
// Both targets are in the left subtree.
root = root->left;
} else {
// root->val is between lo and hi (inclusive): this is the split point.
return root;
}
}
return nullptr; // Problem guarantees p and q exist, so unreachable.
}
};
Time O(H) where H is the height of the BST (O(log n) balanced, O(n) worst-case skewed) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <algorithm>
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int minVal=std::min(p->val, q->val);
int maxVal=std::max(p->val, q->val);
while(root){
if(root->val<minVal){
root=root->right;
}
else if(root->val>maxVal){
root=root->left;
}
else{
return root;
}
}
return nullptr;
}
};
// Time: O(H), where H is the height of the tree, since in the worst case we might have to traverse from the root to a leaf node.
// Space: O(1), since we are using only a constant amount of extra space. My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.