Given the root of a binary tree and a node u in the tree, return the nearest node on the same level that is to the right of u, or return null if u is the rightmost node in its level.
Example 1:
Input: root = [1,2,3,null,4,5,6], u = 4 Output: 5 Explanation: The nearest node on the same level to the right of node 4 is node 5.
Example 2:
Input: root = [3,null,4,2], u = 2 Output: null Explanation: There are no nodes to the right of 2.
Constraints:
[1, 105].1 <= Node.val <= 105u is a node in the binary tree rooted at root.Problem Overview: You are given the root of a binary tree and a target node u. The task is to return the node immediately to the right of u on the same level. If u is the rightmost node in its level, return null. The challenge is identifying nodes that share the same depth while preserving their left‑to‑right order.
Approach 1: Breadth-First Search (Level Order) (Time: O(n), Space: O(n))
This problem maps directly to level order traversal using a queue. Perform a standard Breadth-First Search on the Binary Tree. For each level, track the number of nodes currently in the queue. Iterate through that level from left to right. When you encounter the target node u, check whether it is the last node in the current level. If not, the next node in the queue is the nearest right node. If it is the last node, return null. This works because BFS processes nodes level by level, preserving their natural left-to-right ordering.
The key insight is that the queue already maintains the correct order of nodes at each depth. No extra bookkeeping is required beyond the level size. Each node is visited exactly once, producing O(n) time complexity with O(n) auxiliary space for the queue in the worst case.
Approach 2: Depth-First Search with Level Tracking (Time: O(n), Space: O(n))
A Depth-First Search can also solve the problem by explicitly tracking depth. Traverse the tree and store nodes grouped by their level using a list or hash map keyed by depth. While performing DFS, append each node to the container corresponding to its level. After traversal, locate the level containing node u, then scan that level’s list to find the element immediately after it.
This approach separates traversal from lookup. The DFS builds a level structure first, then a quick index lookup determines the nearest right node. Time complexity remains O(n) since every node is visited once, and space complexity is also O(n) due to the storage of nodes across levels.
Recommended for interviews: The BFS level-order traversal is the expected approach. It aligns naturally with the problem’s definition of "same level" and identifies the neighbor in a single pass without storing all nodes. Showing the DFS variant demonstrates deeper understanding of tree traversal patterns, but BFS communicates the optimal reasoning most clearly.
We can use Breadth-First Search, starting from the root node. When we reach node u, we return the next node in the queue.
The time complexity is O(n), and the space complexity is O(n). Where n is the number of nodes in the binary tree.
Python
Java
C++
Go
JavaScript
DFS performs a pre-order traversal of the binary tree. The first time we search to node u, we mark the current depth d. The next time we encounter a node at the same level, it is the target node.
The time complexity is O(n), and the space complexity is O(n). Where n is the number of nodes in the binary tree.
Python
Java
C++
Go
JavaScript
| Approach | Complexity |
|---|---|
| BFS | — |
| DFS | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| BFS Level Order Traversal | O(n) | O(n) | Best general solution when you need neighbors within the same tree level |
| DFS with Level Storage | O(n) | O(n) | Useful when DFS traversal is already required or when storing nodes grouped by depth |
1602. Find Nearest Right Node in Binary Tree (Leetcode Medium) • Programming Live with Larry • 186 views views
Watch 8 more video solutions →Practice Find Nearest Right Node in Binary Tree with our built-in code editor and test cases.
Practice on FleetCode