# BiTree Cousins

BINARY TREE

DEPTH-FIRST SEARCH

### Problem

Given the `root`

of a binary tree with unique values and the values of two different nodes `x`

and `y`

, this function checks if the nodes corresponding to these values are cousins.

Two nodes are cousins if they have the same depth but different parents.

The function returns `true`

if the nodes are cousins and `false`

otherwise.

### Examples

`areCousins([1,2,3,4,5,6,7], 4, 6) // false /* Why? Node with value 4 has depth 2 and its parent is 2. Node with value 6 has depth 2 and its parent is 3. Even though they are at the same depth, their parents are different. Therefore, they are not cousins. */ areCousins([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 8, 9) // true // Why? Node with value 8 and 9 both have 4 as parent // Therefore, they are not cousins areCousins([1,2,3,4,5,6,7,8,9], 8, 6) // false /* Why? Node with value 8 has depth 3 and its parent is 4. Node with value 6 has depth 2 and its parent is 3. They are at different depths. Therefore, they are not cousins. */`

Loading...

1

2

4

5

3

6

7