Lowest Common Ancestor in BST

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Lowest Common Ancestor in BST

Approach

For BST, both keys below go left, both above go right; otherwise current node is LCA.

Complexity Analysis

Time
O(h) where h is tree height
Space
O(h) recursion depth

Pattern

BST Guided Search

Why It Works

BST ordering guarantees the split of target nodes into left or right partitions.

Updated Mar 2026