In a binary tree, a lonely node is a node that is the only child of its parent node. The root of the tree is not lonely because it does not have a parent node.
Given the root of a binary tree, return an array containing the values of all lonely nodes in the tree. Return the list in any order.
Example 1:
Input: root = [1,2,3,null,4] Output: [4] Explanation: Light blue node is the only lonely node. Node 1 is the root and is not lonely. Nodes 2 and 3 have the same parent and are not lonely.
Example 2:
Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2] Output: [6,2] Explanation: Light blue nodes are lonely nodes. Please remember that order doesn't matter, [2,6] is also an acceptable answer.
Example 3:
Input: root = [11,99,88,77,null,null,66,55,null,null,44,33,null,null,22] Output: [77,55,33,66,44,22] Explanation: Nodes 99 and 88 share the same parent. Node 11 is the root. All other nodes are lonely.
Constraints:
tree is in the range [1, 1000].1 <= Node.val <= 106Problem Overview: Given the root of a binary tree, return all lonely nodes. A node is considered lonely if its parent has exactly one child. In other words, if a parent has only a left child or only a right child, that child is lonely and should be added to the result.
Approach 1: Depth-First Search (DFS) Traversal (O(n) time, O(h) space)
The most direct solution is a recursive DFS over the binary tree. At each node, check whether it has exactly one child. If node.left exists and node.right is null, the left child is lonely. If node.right exists and node.left is null, the right child is lonely. Add the lonely child's value to the result and continue traversing both subtrees.
This approach works because every node is visited exactly once, and the lonely condition depends only on the immediate parent. DFS naturally fits tree problems since recursion mirrors the tree structure. The traversal can be implemented with preorder, inorder, or postorder—any order works since the decision only depends on the current node’s children. Time complexity is O(n) because each node is processed once. Space complexity is O(h), where h is the tree height due to the recursion stack.
DFS is the most common approach for problems involving structural checks in a binary tree. Interviewers often expect this pattern: visit a node, inspect its children, then recurse. If you're comfortable with depth-first search, the implementation is only a few lines.
Approach 2: Breadth-First Search (BFS) with Queue (O(n) time, O(w) space)
The same logic can be implemented using level-order traversal. Use a queue to process nodes layer by layer. For each dequeued node, check whether exactly one child exists. If so, record that child’s value and push it into the queue for further processing.
BFS visits every node once, so the time complexity remains O(n). The space complexity becomes O(w), where w is the maximum width of the tree, because the queue may hold an entire level at once. This approach is useful when you already need level-order traversal or when recursion depth could become large. It relies on the same core idea used in many breadth-first search tree problems.
Recommended for interviews: The DFS approach is usually preferred. It’s concise, easy to reason about, and uses minimal additional data structures. A BFS solution also works and demonstrates familiarity with level-order traversal, but recursive DFS tends to be the cleanest implementation. Showing both approaches signals strong understanding of tree traversal patterns.
We can use Depth-First Search (DFS) to traverse the entire tree. We design a function dfs, which traverses each node in the tree. If the current node is a lone child, we add its value to the answer array. The execution process of the function dfs is as follows:
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Depth-First Search (Recursive) | O(n) | O(h) | Preferred approach for most interviews; concise recursion and natural for tree traversal. |
| Breadth-First Search (Queue) | O(n) | O(w) | Useful when performing level-order traversal or avoiding deep recursion. |
Find All The Lonely Nodes || Leetcode || Algorithms and Data Structures • Ashwanth Pendyala • 1,676 views views
Watch 9 more video solutions →Practice Find All The Lonely Nodes with our built-in code editor and test cases.
Practice on FleetCode