Tap here to switch tabs
Problem
Submissions

Problem: Cousins in Binary Tree

med
30 min
Try to solve the Cousins in Binary Tree problem.

Statement

Given the root of a binary tree and two distinct values x and y, determine whether the nodes with values x and y are cousins.

Two nodes are cousins if they are at the same depth in the tree but have different parent nodes. Return true if they are cousins, otherwise return false.

Note: The values x and y are guaranteed to appear somewhere in the tree.

Constraints:

  • The number of nodes in the tree is in the range [2,100][2, 100]

  • 1≤1 \leq Node.val ≤100\leq 100

  • Each node value in the tree is unique

  • x ≠\neq y

  • x and y exist in the tree

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Cousins in Binary Tree

med
30 min
Try to solve the Cousins in Binary Tree problem.

Statement

Given the root of a binary tree and two distinct values x and y, determine whether the nodes with values x and y are cousins.

Two nodes are cousins if they are at the same depth in the tree but have different parent nodes. Return true if they are cousins, otherwise return false.

Note: The values x and y are guaranteed to appear somewhere in the tree.

Constraints:

  • The number of nodes in the tree is in the range [2,100][2, 100]

  • 1≤1 \leq Node.val ≤100\leq 100

  • Each node value in the tree is unique

  • x ≠\neq y

  • x and y exist in the tree