← 返回 amazon 的题目列表Lowest Common Ancestor with Multiple Children
类型:online_judge
amazon
Given an n-ary tree and two nodes in this tree, write an algorithm to find the lowest common ancestor (LCA) of these two nodes. Each node in the tree can have multiple children.
Input Description:
The root node of the tree.
Two distinct nodes.
Output Description:
Output the lowest common ancestor node.
Example:
Consider the following tree structure:
A
/ | \
B C D
/ \ \
E F G
If the input two nodes are E and F, the output should be B. If the input two nodes are E and G, the output should be A.
Constraints:
The number of nodes in the tree does not exceed 10^4.
The tree is a valid n-ary tree.
Example
Input
A, B, C, D, E, F, G
E, F
E, G