Community Problem
Cousins in Binary Tree
Difficulty: Easy
Decide whether two values in a binary tree are cousins (same depth, different parents).
Cousins in Binary Tree
Decide whether two values in a binary tree are cousins (same depth, different parents).
By @arjunpatel
March 14, 2026
·
Updated May 18, 2026
655 views
20
4.4 (12)
I got this on a phone screen for a senior front-end role, and the interviewer specifically wanted me to explain why BFS is the natural fit before writing code. The question was a litmus test for whether I could read "same depth, different parent" as a level-order condition rather than a path-comparison condition. The catalog covers level-order traversal, but it skipped the variant that forces you to track parents alongside depths.
Cousins in Binary Tree
In a binary tree, two nodes are cousins if they share the same depth but have different parents. The depth of the root is 0. Given the root of a binary tree and two distinct values x and y, return true if and only if the nodes with values x and y are cousins.
It is guaranteed that every value in the tree is unique, that x and y both appear in the tree, and that x != y.
Examples
Example 1:
- Input:
root = [1, 2, 3, 4],x = 4,y = 3 - Output:
false - Explanation: Node
4is at depth2and node3is at depth1. Different depths means they cannot be cousins.
Example 2:
- Input:
root = [1, 2, 3, null, 4, null, 5],x = 5,y = 4 - Output:
true - Explanation: Both nodes are at depth
2. Their parents are3and2respectively, so they are cousins.
Example 3:
- Input:
root = [1, 2, 3, null, 4],x = 2,y = 3 - Output:
false - Explanation: Same depth (
1) but they share the same parent (the root), so they are siblings, not cousins.
Example 4:
- Input:
root = [1, 2, 3, 4, 5, 6, 7],x = 4,y = 7 - Output:
true - Explanation: Both at depth
2, parents are2and3respectively.
Constraints
- The number of nodes in the tree is in the range
[2, 100]. 1 <= Node.val <= 100.- Each node has a unique value.
x != yand bothxandyare values in the tree.
Follow-up
Can you do it in a single BFS pass without storing parent pointers, by only tracking the parent at the time you discover each target?
Solution
Hints
Solution Walkthrough
Approach: Level-Order BFS Tracking Parents (O(n) time, O(w) space)
"Same depth, different parents" is a level-order condition. BFS naturally walks the tree depth by depth, so we let it do the depth bookkeeping for us. The only twist is that the queue stores (node, parent) tuples instead of bare nodes, because we still need to compare parents once both targets are found.
Algorithm
- Push
(root, null)into the queue. - While the queue is non-empty, process one full level at a time:
- Record
parentXif any node at this level has valuex. - Record
parentYif any node at this level has valuey. - Append children with their parent to the next-level queue.
- After the level finishes, three cases:
- Both
parentXandparentYset: returnparentX !== parentY. - Exactly one set: return
false(the other lives at a different depth). - Neither set: continue to the next level.
Why It Works
Processing exactly one level per outer iteration is what makes the depth check implicit: any two nodes captured in the same iteration are guaranteed to be at the same depth, and any two captured in different iterations are guaranteed to be at different depths. The early return when only one is found prevents pointlessly scanning further levels. The parent identity check is reference-based (!== in JS, is not in Python), so we never confuse two distinct parents that happen to share a value (the problem rules out value collisions, but the reference check is robust regardless).
Complexity
Sample tree
1
/ \
2 3
\ \
4 5Metric Value
Time O(n) where n is the node count; each node is enqueued once
Space O(w) where w is the maximum width of the treePitfalls
- Comparing parents by value. If two parents had the same value, value-equality would lie. Compare references (
!==/is not) to be safe. - Returning before both depths are checked. A common bug is to short-circuit when one target is found. The check has to happen after the full level finishes; otherwise you can miss the other target on the same level.
- Treating root as a target. The root has depth 0 and no parent, so it can never be a cousin of anything. The algorithm handles this naturally because if either
xoryequals the root, only one ofparentX/parentYgets set on level 0 and we returnfalse.
Solution Code
