The Find All The Lonely Nodes interview question introduces a simple but interesting tree property. In a binary tree, a node is considered "lonely" if it is the only child of its parent. In other words, its parent has exactly one child. Your goal is to traverse the tree and return a list containing the values of all such lonely nodes.
This "Easy" difficulty question is frequently asked by Microsoft to test a candidate's baseline proficiency with tree traversal. It evaluations whether you can correctly identify parent-child relationships and handle null checks. The Binary Tree interview pattern here checks for clarity in recursive logic and the ability to implement a traversal (either BFS or DFS) without overcomplicating the search condition.
This problem is solved using Depth-First Search (DFS) or Breadth-First Search (BFS). As you traverse the tree, for every node you visit, you check its children:
Consider this tree:
1
/
2 3
4
For tree problems involving properties of a node relative to its parent, it's often easier to make the decision at the parent node. This allows you to check both children simultaneously and avoid passing extra "parent" state through your recursive function.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Average of Levels in Binary Tree | Easy | Solve | |
| Sum of Left Leaves | Easy | Solve | |
| Cousins in Binary Tree | Easy | Solve | |
| Merge Two Binary Trees | Easy | Solve | |
| Minimum Depth of Binary Tree | Easy | Solve |