Lowest Common Ancestor
Medium
Return the lowest common ancestor (LCA) of two nodes, p and q, in a binary tree. The LCA is defined as the lowest node that has both p and q as descendants. A node can be considered an ancestor of itself.
Example:
Constraints:
-
The tree contains at least two nodes.
-
All node values are unique.
-
pandqrepresent different nodes in the tree.