← 返回 meta 的题目列表Lowest Common Ancestor (LCA)
类型:online_judge
Given the root of a binary tree root and two nodes p and q in the tree, return their Lowest Common Ancestor (LCA).
A node x is the LCA of p and q if:
x is an ancestor of both p and q, and
x has the greatest depth among all common ancestors.
Requirements:
Return the LCA node (or its value).
Do not modify the tree.
Constraints:
1 <= n <= 2 * 10^5
Examples:
Tree [3,5,1,6,2,0,8,null,null,7,4], p=5, q=1 -> 3
Same tree, p=5, q=4 -> 5
Example
Input
[3,5,1,6,2,0,8,null,null,7,4]
5 1
Output
3