Understanding the Problem
We are given a Binary Search Tree (BST) and two nodes p
and q
. Our goal is to find their Lowest Common Ancestor (LCA). In a BST, the LCA of two nodes is the lowest node in the tree that has both p
and q
as descendants (a node can be a descendant of itself).
Because this is a BST, the left child of a node is always smaller and the right child is always larger. We can use this property to efficiently find the LCA by comparing p
and q
with the current node while traversing.
Step-by-Step Solution with Example
Step 1: Understand the structure of BST
Let’s use the following BST as an example:
6
/ 2 8
/ / 0 4 7 9
/ 3 5
Step 2: Compare nodes with current root
We start from the root node (6). Suppose we are asked to find the LCA of nodes 2 and 8.
Since 2 < 6 and 8 > 6, they lie on different sides of 6. That means 6 is the first node that splits their paths — this is our LCA.
Step 3: Move left or right if both nodes are on same side
Now suppose we are looking for the LCA of nodes 2 and 4. Both nodes are < 6, so we move left to node 2.
At node 2, we check again: 2 == p and 4 is in the right subtree — so 2 is the ancestor of 4. That means 2 is the LCA.
Step 4: If the current node is equal to one of the targets
Now let’s say we look for the LCA of node 2 and node 2 (both are the same). Here, since both nodes are equal, the LCA is just node 2.
Step 5: Return null if tree is empty
If the tree is empty (i.e., the root is null), we cannot find an ancestor. So, we immediately return null.
Edge Cases
Case 1: Nodes on different sides
This is the most common case. If p < root < q
or q < root < p
, then the current root is the LCA.
Case 2: One node is ancestor of the other
If one node lies directly in the path of the other, then that node is the LCA. For example, LCA of 2 and 4 is 2.
Case 3: Both nodes are the same
If p == q
, then the LCA is just that node. This should be explicitly checked to avoid unnecessary traversal.
Case 4: Tree is empty
If root is null, return null immediately — the tree doesn't contain any nodes.
Finally
By taking advantage of the BST properties, we can find the LCA in O(h) time, where h is the height of the tree. Always check for the edge cases — especially null roots or when both nodes are the same. For beginners, the key intuition is to imagine how you'd search for each node in a BST. The first split point during this process is your lowest common ancestor.
Comments
Loading comments...