Problem Brief
Tree Node Relationship
FULLTIMENEW GRADPHONE SCREEN
See Amazon online assessment and hiring insightsYou 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".
1Example 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.
2Example 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.
3Example 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.
Constraints
Limits and guarantees your solution can rely on.
The input graph is a valid tree rooted at node 0. Both queried nodes are valid node ids.