← 返回 amazon 的题目列表Lowest Common Ancestor of a Binary Tree
类型:online_judge
Given the root of a binary tree and two distinct nodes p and q in the tree, return their lowest common ancestor (LCA).
The LCA is the deepest node that has both p and q as descendants. A node is allowed to be a descendant of itself.
Input format
Line 1: Level-order traversal of the tree, comma-separated; missing nodes are represented by null.
Line 2: Value of node p.
Line 3: Value of node q.
Node values are unique, and both p and q exist in the tree.
Output format
Print the value of the LCA.
Constraints
Number of nodes: 2 to 10^5.
Node values: -10^9 to 10^9.
Example
Input
3,5,1,6,2,0,8,null,null,7,4
5
1
Output
3