← 返回 amazon 的题目列表Determine Relationship Between Two Nodes in a Tree (Siblings / Cousins / Other)
类型:online_judge
Determine the Relationship Between Two Nodes in a Tree (Siblings / Cousins / Other)
Given a tree with root root and two distinct node values x and y (guaranteed to exist uniquely in the tree), return their relationship:
"siblings": same parent.
"cousins": same depth but different parents.
"others": otherwise.
Input
Line 1: a binary tree in level-order array form using null for missing children, e.g. [1,2,3,null,4]
Line 2: two integers x y
Output
One of: siblings, cousins, others.
Constraints
1 <= n <= 1e5
1 <= Node.val <= 1e9
Example 1
Input:
[1,2,3,4,null,null,5]
4 5
Output:
cousins
Example 2
Input:
[1,2,3]
2 3
Output:
siblings
Example
Input
[1,2,3]
2 3
Output
siblings