Lowest Common Ancestor of a Binary Tree

Medium · Trees · about 20 minutes

Asked at Meta, Microsoft, Amazon, LinkedIn.

Two values p and q both occur in a binary tree. Find the node furthest from the root whose subtree contains both of them, and return its value. A node's subtree includes the node itself, so if p sits above q, the answer is p.

Related guides