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 = 3
Output:false
Explanation:

Different depths.

Input:root = [1,2,3,null,4,null,5], x = 5, y = 4
Output:true
Explanation:

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 = 5
Output:false
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!