Skip to main content

Find All The Lonely Nodes - Solution & Explanation

EasyPremiumFree on FleetCodeTreeDepth-First SearchBreadth-First SearchBinary Tree10 min readAsked at: Microsoft
Practice this problem

Problem Statement

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:

  • The number of nodes in the tree is in the range [1, 1000].
  • 1 <= Node.val <= 106

Approach Overview

Problem 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.

Solution

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:

  1. If the current node is null, or the current node is a leaf node (i.e., both the left and right children of the current node are null), then return directly.
  2. If the left child of the current node is null, then the right child of the current node is a lone child, and we add its value to the answer array.
  3. If the right child of the current node is null, then the left child of the current node is a lone child, and we add its value to the answer array.
  4. Recursively traverse the left and right children of the current node.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen 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.

Video Solution

Find All The Lonely Nodes || Leetcode || Algorithms and Data StructuresAshwanth Pendyala1,679 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find All The Lonely Nodes easy or hard?
Find All The Lonely Nodes is classified as an Easy problem. The main challenge is recognizing that a simple tree traversal with a parent-child check is sufficient, making it a good practice problem for binary tree fundamentals.
Find All The Lonely Nodes Python/Java solution
A typical Python or Java solution performs a DFS traversal starting from the root. At each node, check if only one child exists and append that child’s value to a result list. Continue recursively for both children until the entire tree is explored.
How to solve Find All The Lonely Nodes in O(n)?
Traverse the binary tree using DFS or BFS. For every node, check if exactly one child exists. If the left child exists but the right does not, record the left node; if the right exists but the left does not, record the right node. Continue traversal until all nodes are processed.
What is the best approach for Find All The Lonely Nodes?
Depth-First Search (DFS) is the most common approach. Traverse the binary tree and check each node’s children. If a node has exactly one child, add that child’s value to the result. DFS visits every node once, giving O(n) time complexity with O(h) recursion stack space.
Is Find All The Lonely Nodes asked at Google/Amazon/Meta?
This problem represents a common binary tree traversal pattern often seen in technical interviews. Variations of parent-child relationship checks appear in interviews at large tech companies including Google, Amazon, and Meta, especially in easy-to-medium tree rounds.
What data structure is used in Find All The Lonely Nodes?
The primary data structure is a binary tree. The algorithm uses either recursion (DFS) with the call stack or an explicit queue for BFS traversal to visit each node and inspect its children.
What is the time complexity of Find All The Lonely Nodes?
The time complexity is O(n), where n is the number of nodes in the binary tree. Each node is visited exactly once during the traversal, and checking whether a node has one child takes constant time.

Ready to solve this problem?

Practice Find All The Lonely Nodes with our built-in code editor and test cases.

Practice on FleetCode