In the Cousins in Binary Tree interview question, you are given the root of a binary tree and two node values and . Two nodes are considered "cousins" if they satisfy two conditions:
true if and are cousins, and false otherwise.Microsoft and Amazon use the Cousins in Binary Tree coding problem to assess a candidate's ability to track multiple pieces of information (depth and parent) during a tree traversal. It’s an "Easy" difficulty problem that evaluates whether you can use Breadth-First Search (BFS) or Depth-First Search (DFS) to capture and compare node metadata.
This problem follows a Tree Traversal pattern.
(node, parent) pairs is a common implementation.(depth, parent) for both and in variables or a map. After the traversal, compare the stored values.Imagine a tree:
1
/
2 3
/
4 5
Target nodes: 4 and 5.
true.false as soon as you find at one depth, without checking where is.BFS is often the most natural choice for "same depth" problems because it naturally groups nodes by level. If you use DFS, remember to pass both the current depth and the parent node as arguments in the recursive calls.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Depth of Binary Tree | Easy | Solve | |
| Path Sum | Easy | Solve | |
| Average of Levels in Binary Tree | Easy | Solve | |
| Sum of Left Leaves | Easy | Solve | |
| Symmetric Tree | Easy | Solve |