← 返回 meta 的题目列表Lowest Common Ancestor with Duplicates
类型:online_judge
meta
Given a binary tree, where each node's value is unique, find the lowest common ancestor (LCA) of two given nodes. If the tree contains duplicate node values, return the LCA of the bottommost duplicated node.
Example:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The lowest common ancestor of nodes 5 and 1 is 3.
Input: root = [3,5,1,6,2,0,8,null,null,7,4,4], p = 5, q = 4
Output: 5
Explanation: The lowest common ancestor of nodes 5 and 4 is 5.
Notes:
All node values are unique (unless specified otherwise).
p and q are different and both exist in the tree.
Example
Input
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1