Lowest Common Ancestor of a Binary Search Tree
Given the root of a Binary Search Tree (BST) and two nodes p and q, find their Lowest Common Ancestor (LCA).
The BST property is:
Left Subtree < Root < Right Subtree
The Lowest Common Ancestor is the deepest node that has both p and q in its subtree.
Important Difference
Unlike the normal Binary Tree LCA problem, here we can use the BST property to decide whether to move left or right.
Example Tree
6
/ \
2 8
/ \ / \
0 4 7 9
/ \
3 5
For:
p = 2 q = 8
The LCA is:
6
Because 2 < 6 and 8 > 6, the two nodes lie on opposite sides of 6.
Example 1
Input
root = [6,2,8,0,4,7,9,null,null,3,5] p = 2 q = 8
Output
6
Explanation
The BST is:
6
/ \
2 8
/ \ / \
0 4 7 9
/ \
3 5
We start at 6.
p = 2 < 6 q = 8 > 6
Since one node is on the left and the other is on the right, 6 is their Lowest Common Ancestor.
Example 2
Input
root = [6,2,8,0,4,7,9,null,null,3,5] p = 2 q = 4
Output
2
Explanation
Start at 6.
Both nodes are smaller than 6:
2 < 6 4 < 6
So move to the left subtree.
Now we are at 2.
p = 2 q = 4
Since 2 itself is one of the target nodes and 4 lies in its right subtree, 2 is the LCA.
Constraints
Hints:
Hint 1
Use the BST property instead of searching the entire tree.
Hint 2
If both p and q are greater than the current node, move:
Current → Right
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 13, 2026
Last updated
Aug 14, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output