1676. Lowest Common Ancestor of a Binary Tree IV
Last updated
Last updated
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv
Given the root
of a binary tree and an array of TreeNode
objects nodes
, return the lowest common ancestor (LCA) of all the nodes in nodes
. All the nodes will exist in the tree, and all values of the tree's nodes are unique.
Extending the definition of LCA on Wikipedia: "The lowest common ancestor of n
nodes p1
, p2
, ..., pn
in a binary tree T
is the lowest node that has every pi
as a descendant (where we allow a node to be a descendant of itself) for every valid i
". A descendant of a node x
is a node y
that is on the path from node x
to some leaf node.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
The number of nodes in the tree is in the range [1, 104]
.
-109 <= Node.val <= 109
All Node.val
are unique.
All nodes[i]
will exist in the tree.
All nodes[i]
are distinct.