Description
Two nodes are cousins if they are at the same depth but have different parents. Given root of a binary tree with unique values and two values x and y, return true if x and y are cousins.
Examples
Input:
root = [1,2,3,4], x = 4, y = 3Output:
falseExplanation:
Different depths.
Input:
root = [1,2,3,null,4,null,5], x = 5, y = 4Output:
trueExplanation:
Nodes 4 and 5 are at the same depth (3) and have different parents (2 and 3), so they are cousins.
Input:
root = [1,2,3,4,5,6,7], x = 4, y = 5Output:
falseExplanation:
Nodes 4 and 5 are at the same depth (level 2) but they have the same parent (node 2), so they are siblings, not cousins.
Constraints
- •
2 ≤ nodes ≤ 100