Given a binary tree and two distinct node values, find the lowest common ancestor node of the two values. The lowest common ancestor is the node farthest from the root that has both values in its subtree. If one value does not exist in the tree, return null.
Explanation: The lowest common ancestor of 5 and 1 is 3, as it is the farthest node from the root that has both values in its subtree.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak