Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Cousins in Binary Tree
On this page

Problem Statement

Given the root of a binary tree with unique values and two different nodes x and y, return a boolean value based on whether the nodes corresponding to the values x and y are cousins.

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

Examples

Example 1:

  • Input: root = [1, 2, 3, 4, null, 5, 6], x = 4, y = 5
    1
   / \
  2   3
 /   / \
4   5   6  
  • Expected Output: true
  • Justification: Nodes 4 and 5 both are at depth 2 with parent 2

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page