Given a binary tree and a node in it, write an efficient algorithm to find its next node at the same level as the node.

For example, consider the following binary tree:

 
Find Next Node in Binary Tree

The next node of 2 is 3
The next node of 5 is 6
The next node of 7 is 8
The next node of 8 is null

Practice this problem

A simple solution is to perform a level order traversal on the tree. The idea is to modify the level order traversal to maintain the level number of each node, and if the given node is found, we return its immediate right node, present at the same level.

The implementation can be seen below in C++, Java, and Python:

C++


Download  Run Code

Output:

Right node is 6

Java


Download  Run Code

Output:

Right node is 6

Python


Download  Run Code

Output:

Right node is 6

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the binary tree.
 

 
We can also solve this problem by using constant auxiliary space and linear time. The idea is to traverse the tree in a preorder fashion and search for the given node. Once the node is found, mark its level number. Then the first node encountered at the same level is the next right node.

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

Right node is 6

Java


Download  Run Code

Output:

Right node is 6

Python


Download  Run Code

Output:

Right node is 6

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(h) extra space for the call stack, where h is the height of the tree.