Amazon·Software Engineer·Technical Phone Screen
- Given the root of a binary tree and two nodes p and q, find their lowest common ancestor. The LCA is the deepest node that has both p and q as descendants, and a node can be a descendant of itself. Solve it recursively with O(N) time and O(H) space.
“The recursive framing is what makes this one actually worth thinking about.”