Check Whether Two Binary Tree Nodes Are Cousins

Quick Overview

Given a binary tree with unique values and two node values, decide whether the two nodes sit at the same depth under different parents. It tests level-order traversal, tracking each node's parent, and handling siblings and the root as edge cases.

Check Whether Two Binary Tree Nodes Are Cousins

Company: Shopify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the `root` of a binary tree with unique values and two different node values `x` and `y`, return `True` if and only if the nodes corresponding to the values `x` and `y` are **cousins**. Two nodes of a binary tree are cousins if they have the same depth but have different parents. Note that in a binary tree, the root node is at depth `0`, and children of each depth `k` node are at depth `k + 1`. ### Function Signature ```python def is_cousins(root: Optional[TreeNode], x: int, y: int) -> bool: ``` ### Rules - `TreeNode` has the fields `val`, `left` and `right`. - The examples write the tree as a level-order list in which `None` marks a missing child. Trailing `None` entries are left out. - Two nodes that share a parent are siblings, and siblings are not cousins even though they have the same depth. ### Constraints - The number of nodes in the tree is in the range 2 to 100. - `1 <= Node.val <= 100` - Each node has a unique value. - `x != y` - `x` and `y` exist in the tree. ### Examples ```text Input: root = [1, 2, 3, 4], x = 4, y = 3 Output: False 1 / \ 2 3 / 4 ``` Node 4 is at depth 2 and node 3 is at depth 1. They are not at the same depth, so they are not cousins. ```text Input: root = [1, 2, 3, None, 4, None, 5], x = 5, y = 4 Output: True 1 / \ 2 3 \ \ 4 5 ``` Nodes 4 and 5 are both at depth 2 and have different parents (2 and 3), so they are cousins. ```text Input: root = [1, 2, 3, None, 4], x = 2, y = 3 Output: False 1 / \ 2 3 \ 4 ``` Nodes 2 and 3 are both at depth 1, but both are children of node 1. They are siblings, not cousins.

Overview: Given a binary tree with unique values and two node values, decide whether the two nodes sit at the same depth under different parents. It tests level-order traversal, tracking each node's parent, and handling siblings and the root as edge cases.

Shopify logo
Shopify
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree with unique values and two different node values x and y, return True if and only if the nodes corresponding to the values x and y are cousins.

Two nodes of a binary tree are cousins if they have the same depth but have different parents.

Note that in a binary tree, the root node is at depth 0, and children of each depth k node are at depth k + 1.

Function Signature

def is_cousins(root: Optional[TreeNode], x: int, y: int) -> bool:

Rules

  • TreeNode has the fields val , left and right .
  • The examples write the tree as a level-order list in which None marks a missing child. Trailing None entries are left out.
  • Two nodes that share a parent are siblings, and siblings are not cousins even though they have the same depth.

Constraints

  • The number of nodes in the tree is in the range 2 to 100.
  • 1 <= Node.val <= 100
  • Each node has a unique value.
  • x != y
  • x and y exist in the tree.

Examples

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

        1
       / \
      2   3
     /
    4

Node 4 is at depth 2 and node 3 is at depth 1. They are not at the same depth, so they are not cousins.

Input: root = [1, 2, 3, None, 4, None, 5], x = 5, y = 4
Output: True

        1
       / \
      2   3
       \   \
        4   5

Nodes 4 and 5 are both at depth 2 and have different parents (2 and 3), so they are cousins.

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

        1
       / \
      2   3
       \
        4

Nodes 2 and 3 are both at depth 1, but both are children of node 1. They are siblings, not cousins.