Cousins in Binary Tree
You are given the root of a binary tree. Two nodes are cousins if they are on the same level but have different parents. Given the values x and y (different, and both in the tree, with all values distinct), return True if they are cousins, otherwise False.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [1,2,3,4]
x 4
y 3
returns false
4 is on level 2, 3 on level 1.
Example 2
root [1,2,3,null,4,null,5]
x 5
y 4
returns true
Same level, different parents (3 and 2).
Constraints
2 <= number of nodes <= 100- Values in the tree are distinct.
x != y, and both are values in the tree.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.