AmazonOA #353
Tree Node Relationship
估计简单评测数据准备中,可先阅读题面
You are given an undirected tree with nodes numbered from 0 to n - 1, rooted at node 0. For two queried nodes, classify their relationship.
Return "siblings" if the two nodes have the same parent. Return "cousins" if they are at the same depth but have different parents. Otherwise, return "others".
Constraints
The input graph is a valid tree rooted at node 0. Both queried nodes are valid node ids.
Example 1
Input:
n = 7
edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
nodeA = 3
nodeB = 4
Output:
"siblings"
Explanation: Nodes 3 and 4 share parent 1.
Example 2
Input:
n = 7
edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
nodeA = 3
nodeB = 5
Output:
"cousins"
Explanation: Nodes 3 and 5 are both depth 2, but their parents are different.
Example 3
Input:
n = 7
edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
nodeA = 1
nodeB = 6
Output:
"others"
Explanation: The two nodes are at different depths.