A binary search tree node is `{ val, left, right }`. Implement `lca(root, p, q)` returning the value of the lowest node that is an ancestor of both values `p` and `q` (a node is an ancestor of itself). Use the BST ordering for an O(height) solution. (Asked at Microsoft and Amazon.)
+ 2 hidden tests run on Submit.
Descend from the root: while both values lie on the same side, move that way. The moment they diverge — or one matches the current node — that node is the lowest common ancestor. The walk costs O(height), which is O(log n) for a balanced BST.
Run your code to see results.