LCA in BST
The BST ordering property makes LCA straightforward. Starting at the root, if both target values are smaller, move left. If both are larger, move right. Otherwise, the current node is the split point and therefore the lowest common ancestor, assuming both target nodes exist in the tree.
If both values are less than the current node, go left.
If both values are greater, go right.
Otherwise the current node is the LCA.
Time complexity: O(h).
Iterative implementation uses O(1) auxiliary space.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience