Walk down until they split
Time O(h) Space O(1)Loop: both smaller → go left; both larger → go right; otherwise return the node.
root = [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5]Step 1/33 and 5 are both < 6: go left.
class Solution {
public int lowestCommonAncestor(TreeNode root, int p, int q) {
TreeNode cur = root;
while (true) {
if (p < cur.val && q < cur.val) cur = cur.left;
else if (p > cur.val && q > cur.val) cur = cur.right;
else return cur.val;
}
}
}Verdict: No recursion, no full search.