Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Cousins in Binary Tree

XPChallenge Points: 10
levelLevel: Easy

You’re given the root of a binary tree (unique values) and two distinct target values x and y.
Two nodes are cousins if they are at the same depth but have different parents.
Return true if the nodes with values x and y are cousins; otherwise return false.
Depth of root is 0.

Example 1:

Input: root = [1,2,3,4], x = 4, y = 3

Output: false

Example 2:

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

Output: true

Example 3:

Input: root = [1,2,3,null,4], x = 2, y = 3

Output: false

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result