LeetCode 1650. Lowest Common Ancestor of a Binary Tree III
Tree
PreviousLeetCode 1644. Lowest Common Ancestor of a Binary Tree IINextLeetCode 1676. Lowest Common Ancestor of a Binary Tree IV
Last updated
Tree
Last updated
Given two nodes of a binary tree p
and q
, return their lowest common ancestor (LCA).
Each node will have a reference to its parent node. The definition for Node
is below:
According to the definition of LCA on Wikipedia: "The lowest common ancestor of two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)."
Example 1:
Example 2:
Example 3:
Constraints:
The number of nodes in the tree is in the range [2, 105]
.
-10^9 <= Node.val <= 10^9
All Node.val
are unique.
p != q
p
and q
exist in the tree.