intermediate9 min read·Updated September 29, 2026

LCA in a BST Explained: Finding LCA for 2, 8 and 2, 4 in [6,2,8,0,4,7,9]

Master Lowest Common Ancestor in BSTs with a clear mental model. Walk through tracing LCA of 2,8 and 2,4 in [6,2,8,0,4,7,9] with code-free intuition.

By Learnisim AI·Published September 29, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Binary Search Tree properties
  • Basic tree traversal recursion/iteration
Lowest Common Ancestor (LCA) in BST [6, 2, 8, 0, 4, 7, 9] BST Node Map & Query Paths 6 2 8 0 4 7 9 Query 1: p = 2, q = 8 Split at root 6 (2 < 6 < 8) => LCA is 6 Query 2: p = 2, q = 4 Both < 6, go left to 2 => LCA is 2 BST Decision Map & Traversal Rules Compare p & q with current node value Three-Way Directional Logic Both < Node Move LEFT Both > Node Move RIGHT Split / Equal Found LCA! O(Height) time complexity vs O(N) brute force No backtracking needed due to BST ordering! Why BST Beats Unordered Trees Strict inequality rules instantly eliminate half the tree at each single comparison step.
LCA of 2 and 8, then of 2 and 4, in BST [6,2,8,0,4,7,9] overview diagram
Why

Why Searching for Shared Ancestors Needs a Shortcut

Phase 1: The Shared Ancestor Problem

Imagine you are handed the binary search tree and asked to find the Lowest Common Ancestor (LCA) for two targets: first, and , and second, and . In a generic binary tree without ordering guarantees, finding where two nodes meet requires scanning the entire structure or recording full root-to-node paths for both targets and comparing them element by element.
That brute-force path-comparison approach works, but it ignores a massive structural advantage. Because this tree is a Binary Search Tree (BST), every node to the left is strictly smaller than the parent, and every node to the right is strictly larger. If you treat the BST like a standard unordered binary tree, you waste valuable traversal steps wandering down branches that have no mathematical bearing on where and diverge.
Without an ordered traversal strategy, locating the divergence point between 2 and 8 forces you to traverse blindly across both halves of the tree before figuring out they sit on opposite sides of the root 6. We need a method that exploits the strict ordering rules of BSTs to pinpoint the split instantly.
Why Searching for Shared Ancestors Needs a Shortcut in BST Tree: [6, 2, 8, 0, 4, 7, 9] | Exploiting BST ordering rules to find Lowest Common Ancestor (LCA) instantly BST Node Topology & LCA Paths 6 2 8 0 4 7 9 Query 1: LCA(2, 8) • 2 < 6 < 8: Split occurs immediately at Root (6). Result: 6 (O(1) decision from root!) Query 2: LCA(2, 4) • Both 2 & 4 < 6 → Go Left to 2. Result: 2 (Node 2 is ancestor of 4) Brute-Force vs. BST Shortcut Approach A: Unordered Binary Tree (Brute Force) 1. Ignore ordering properties completely. 2. Scan entire structure or record full root paths: Path(2): 6 → 2 | Path(8): 6 → 8 • Wastes steps wandering branches with no bearing. • O(N) time complexity scanning unrelated subtrees. Approach B: BST Ordered Shortcut (Optimal) 1. Leverage BST invariant: Left < Root < Right. 2. Compare targets p & q directly against current node: • If both < node: move LEFT • If both > node: move RIGHT • If they split (one smaller, one larger): STOP! Current is LCA. • O(H) time complexity — finds split instantly!
Why Searching for Shared Ancestors Needs a Shortcut diagram
Model

The BST Sorting Property as a Decision Map

Phase 2: The BST Sorting Property as a Decision Map

To understand how to find the Lowest Common Ancestor without wandering through the entire tree, we return to our locked example: the BST . In this tree, 6 sits at the root, its left subtree contains values strictly smaller than 6 (), and its right subtree contains values strictly larger ().
When searching for , our target nodes 2 and 8 lie on opposite sides of the root value 6. Node 2 is smaller than 6, and node 8 is larger. This divergence point at the root is precisely what makes 6 the lowest common ancestor.
By contrast, when searching for , both target nodes are smaller than the root 6. They both live in the left subtree. Therefore, the root 6 cannot be the divergence point; we must step left to examine the subtree rooted at 2.
6 <-- LCA(2, 8)
/ \
2 8
/ \
0 4 <-- LCA(2, 4)
This reveals a strict three-way directional logic at any current node during our traversal. If both target values are smaller than the current node, move left. If both are larger, move right. The exact moment this rule fails—where one target is and the other is , or when we land directly on one of the targets—is our LCA.
Try this: Given currentNode = 6, p = 2, q = 4:
1.Compare p and q to currentNode.
2.Determine whether to go left, go right, or stop.
BST Lowest Common Ancestor (LCA) Decision Map Tree: [6, 2, 8, 0, 4, 7, 9] BST Structure & Query Paths 6 2 8 0 4 7 9 LCA(2,8) LCA(2,4) left subtree (<6) Three-Way Decision Rule < Both p & q < Current Node Action: Move LEFT (e.g., LCA(2,4) at root 6) > Both p & q > Current Node Action: Move RIGHT (both lie in right subtree) ⚡ Split / Divergence Point (LCA) One ≤ current & one ≥ current (e.g. LCA(2,8) at 6) Key Takeaway for LCA(2, 8) • Node 2 is smaller than 6 (goes left) • Node 8 is larger than 6 (goes right) → Root 6 immediately splits them: 6 is the LCA!
The BST Sorting Property as a Decision Map diagram
Worked example

Tracing LCA(2, 8) and LCA(2, 4) Step-by-Step

Phase 3: Walking the Search Path

To see the decision map in action, we will trace two distinct queries on our canonical Binary Search Tree: . The structure of this tree places 6 at the root, with left subtree rooted at 2 (having children 0 and 4) and right subtree rooted at 8 (having children 7 and 9). We want to find the Lowest Common Ancestor for and , and then for and .
Given:
- Root node: 6
- Left subtree: [2, 0, 4], Right subtree: [8, 7, 9]
- Query 1: ,
- Query 2: ,
Steps for Query 1: LCA(2, 8)
1. Start at Root (6): Compare and with current node value 6. Since and , the values sit on opposite sides of the root. The split happens right here.
2. Result for Query 1: Return node 6 as the LCA because it is the exact divergence point.
Steps for Query 2: LCA(2, 4)
1. Start at Root (6): Compare and with current node value 6. Both values are strictly less than 6 ( and ). Take the left branch to node 2.
2. At Node (2): Compare and with current node value 2. Here, equals the current node value, while is greater than 2. Because they no longer share the same direction (one is equal, one is greater), we have found our split point.
3. Result for Query 2: Return node 2 as the LCA. Notice that node 2 is an ancestor of node 4, satisfying the BST LCA rule where a node can be the ancestor of another.
Try this: Given the BST [6, 2, 8, 0, 4, 7, 9], trace the exact node visits and comparisons when finding LCA(4, 9). Which node triggers the split condition?
Phase 3: Walking the Search Path - LCA(2, 8) and LCA(2, 4) BST [6, 2, 8, 0, 4, 7, 9] & Query Paths 6 2 8 0 4 7 9 LCA(2, 8): 2 < 6 < 8 (Split at Root 6) LCA(2, 4): Both < 6, move to 2 (Split at Node 2) Divergence point = Lowest Common Ancestor Query 1: LCA(p=2, q=8) 1 Start at Root (6): 2 < 6 and 8 > 6 2 Values sit on opposite sides (split!) Result: Return Node 6 as LCA Query 2: LCA(p=2, q=4) 1 Root (6): Both < 6. Go left to node 2. 2 Node (2): p=2 equals current, q=4 > 2 (Split!) Result: Return Node 2 as LCA
Tracing LCA(2, 8) and LCA(2, 4) Step-by-Step diagram
Practice

Practice Predicting the LCA Split Point

Phase 4: Practice Your Mental Search Path

Now that you have seen how 6 splits 2 and 8 and how 2 acts as an ancestor for 4 in the BST , let's test your ability to execute this mental model on a slightly different pair.
Imagine you are asked to find the Lowest Common Ancestor for and within this same BST. Recall that 0 and 4 reside in the left subtree rooted at 2. Without writing any code, trace the comparison steps starting from the root node 6, determine which node causes the search paths of 0 and 4 to diverge, and identify the final returned LCA node.
Take a moment to mentally evaluate the comparisons at the root 6 and the left child 2, then check your reasoning against the structural layout of the tree.
Try this: Given the BST [6, 2, 8, 0, 4, 7, 9], what is the step-by-step path and final returned node for LCA(0, 4)?
Apply

Applying the BST Split Point Pattern to Unbalanced Trees

Phase 5: Applying the BST Split Point Pattern to Unbalanced Trees

We started with our BST where LCA(2, 8) split at the root 6, and LCA(2, 4) terminated early at node 2 because 2 was an ancestor of 4. The core pattern we extracted is that the LCA is always the first node whose value falls inclusively between and . Now, let us apply this exact mechanism to a new scenario where the BST shape is distorted.
Imagine our BST is modified so that the left subtree of 2 is completely missing, and node 4 is attached directly as the left child of 6, making the tree skewed. If we want to find in this modified tree, our directional decision rule remains identical. We start at root 6. Since both 0 and 4 are less than 6, we move left to node 2. At node 2, 0 is less than 2, but 4 is greater than 2. The path diverges right here, meaning node 2 is our split point and thus our LCA.
The real power of this logic is that its time complexity remains where is the tree height, and space complexity drops to if implemented iteratively. Whether the tree is balanced like our initial layout or skewed, the comparison map never changes because the BST invariant guarantees that all values in the left subtree are smaller and all values in the right subtree are larger.
python
def lowestCommonAncestor(root, p, q):
    curr = root
    while curr:
        if p.val < curr.val and q.val < curr.val:
            curr = curr.left
        elif p.val > curr.val and q.val > curr.val:
            curr = curr.right
        else:
            return curr
    return None

FAQ

Why is the LCA of 2 and 8 equal to 6 in the BST [6,2,8,0,4,7,9]?
Node 6 is the split point where 2 lies in the left subtree and 8 lies in the right subtree, making it their Lowest Common Ancestor.
How does the BST property simplify finding the LCA compared to a standard binary tree?
In a BST, node values are ordered, allowing us to use a decision map to navigate left or right in O(h) time without needing parent pointers.
What is the time and space complexity of finding an LCA in a BST?
The time complexity is O(h) where h is the tree height (O(log n) for balanced BSTs), and space complexity is O(1) iteratively or O(h) recursively.
What happens if one node is an ancestor of the other, like LCA(2, 4) in the example?
When one target node equals the current node (e.g., 2), the search stops immediately, and that node is returned as the LCA because it is an ancestor of 4.

Keep learning