Given the root of a binary tree with unique values and the values of two different nodes x and y, return true if the nodes are cousins: they sit at the same depth but have different parents. The root is at depth 0 and the children of a node at depth k are at depth k + 1.
Given the root of a binary tree with unique values and the values of two different nodes x and y, return true if the nodes are cousins: they sit at the same depth but have different parents. The root is at depth 0 and the children of a node at depth k are at depth k + 1.
Example 1:
Input: {"root":[1,2,3,4],"x":4,"y":3}
Output: false
Constraints:
The number of nodes in the tree is in the range [2,100].
1≤Node.val≤100
Each node has a unique value.
x=y
x and y exist in the tree.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"root":[1,2,3,4],"x":4,"y":3}
Cousins share a depth but not a parent. Walk the tree one level at a time and watch for 4 and 3.