Given a binary tree and a node, we need to write a program to find inorder successor of this node.
Inorder Successor of a node in binary tree is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.

In the above diagram, inorder successor of node 4 is 2 and node 5 is 1.
We have already discussed how to find the inorder successor of a node in Binary Search Tree. We can not use the same approach to find the inorder successor in general Binary trees.
We need to take care of 3 cases for any node to find its inorder successor as described below:
- Right child of node is not NULL. If the right child of the node is not NULL then the inorder successor of this node will be the leftmost node in it’s right subtree.
- Right Child of the node is NULL. If the right child of node is NULL. Then we keep finding the parent of the given node x, say p such that p->left = x. For example in the above given tree, inorder successor of node 5 will be 1. First parent of 5 is 2 but 2->left != 5. So next parent of 2 is 1, now 1->left = 2. Therefore, inorder successor of 5 is 1.
Below is the algorithm for this case: - Suppose the given node is x. Start traversing the tree from root node to find x recursively.
- If root == x, stop recursion otherwise find x recursively for left and right subtrees.
- Now after finding the node x, recursion will backtrack to the root. Every recursive call will return the node itself to the calling function, we will store this in a temporary node say temp.Now, when it backtracked to its parent which will be root now, check whether root.left = temp, if not , keep going up
- If node is the rightmost node. If the node is the rightmost node in the given tree. For example, in the above tree node 6 is the right most node. In this case, there will be no inorder successor of this node. i.e. Inorder Successor of the rightmost node in a tree is NULL.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node* temp = new Node;
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Node* leftMostNode(Node* node)
{
while (node != NULL && node->left != NULL)
node = node->left;
return node;
}
Node* rightMostNode(Node* node)
{
while (node != NULL && node->right != NULL)
node = node->right;
return node;
}
Node* findInorderRecursive(Node* root, Node* x )
{
if (!root)
return NULL;
if (root==x || (temp = findInorderRecursive(root->left,x)) ||
(temp = findInorderRecursive(root->right,x)))
{
if (temp)
{
if (root->left == temp)
{
cout << "Inorder Successor of " << x->data;
cout << " is " << root->data << "\n" ;
return NULL;
}
}
return root;
}
return NULL;
}
void inorderSuccessor(Node* root, Node* x)
{
if (x->right != NULL)
{
Node* inorderSucc = leftMostNode(x->right);
cout<< "Inorder Successor of " <<x->data<< " is " ;
cout<<inorderSucc->data<< "\n" ;
}
if (x->right == NULL)
{
Node* rightMost = rightMostNode(root);
if (rightMost == x)
cout << "No inorder successor! Right most node.\n" ;
else
findInorderRecursive(root, x);
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
inorderSuccessor(root, root);
inorderSuccessor(root, root->left->left);
inorderSuccessor(root, root->right->right);
return 0;
}
|
Java
class Solution
{
static class Node
{
int data;
Node left, right;
}
static Node temp = new Node();
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static Node leftMostNode(Node node)
{
while (node != null && node.left != null )
node = node.left;
return node;
}
static Node rightMostNode(Node node)
{
while (node != null && node.right != null )
node = node.right;
return node;
}
static Node findInorderRecursive(Node root, Node x )
{
if (root== null )
return null ;
if (root==x || (temp = findInorderRecursive(root.left,x))!= null ||
(temp = findInorderRecursive(root.right,x))!= null )
{
if (temp!= null )
{
if (root.left == temp)
{
System.out.print( "Inorder Successor of " +x.data);
System.out.print( " is " + root.data + "\n" );
return null ;
}
}
return root;
}
return null ;
}
static void inorderSuccessor(Node root, Node x)
{
if (x.right != null )
{
Node inorderSucc = leftMostNode(x.right);
System.out.print( "Inorder Successor of " +x.data+ " is " );
System.out.print(inorderSucc.data+ "\n" );
}
if (x.right == null )
{
int f = 0 ;
Node rightMost = rightMostNode(root);
if (rightMost == x)
System.out.print( "No inorder successor! Right most node.\n" );
else
findInorderRecursive(root, x);
}
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right.right = newNode( 6 );
inorderSuccessor(root, root.right);
inorderSuccessor(root, root.left.left);
inorderSuccessor(root, root.right.right);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def leftMostNode(node):
while (node ! = None and node.left ! = None ):
node = node.left
return node
def rightMostNode(node):
while (node ! = None and node.right ! = None ):
node = node.right
return node
def findInorderRecursive(root, x ):
if ( not root):
return None
if (root = = x or (findInorderRecursive(root.left, x)) or
(findInorderRecursive(root.right, x))):
if findInorderRecursive(root.right, x):
temp = findInorderRecursive(root.right, x)
else :
temp = findInorderRecursive(root.left, x)
if (temp):
if (root.left = = temp):
print ( "Inorder Successor of" ,
x.data, end = "")
print ( " is" , root.data)
return None
return root
return None
def inorderSuccessor(root, x):
if (x.right ! = None ) :
inorderSucc = leftMostNode(x.right)
print ( "Inorder Successor of" , x.data,
"is" , end = " " )
print (inorderSucc.data)
if (x.right = = None ):
f = 0
rightMost = rightMostNode(root)
if (rightMost = = x):
print ( "No inorder successor!" ,
"Right most node." )
else :
findInorderRecursive(root, x)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.right = newNode( 6 )
inorderSuccessor(root, root.right)
inorderSuccessor(root, root.left.left)
inorderSuccessor(root, root.right.right)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
}
public static Node temp = new Node();
public static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static Node leftMostNode(Node node)
{
while (node != null &&
node.left != null )
{
node = node.left;
}
return node;
}
public static Node rightMostNode(Node node)
{
while (node != null &&
node.right != null )
{
node = node.right;
}
return node;
}
public static Node findInorderRecursive(Node root,
Node x)
{
if (root == null )
{
return null ;
}
if (root == x ||
(temp = findInorderRecursive(root.left, x)) != null ||
(temp = findInorderRecursive(root.right, x)) != null )
{
if (temp != null )
{
if (root.left == temp)
{
Console.Write( "Inorder Successor of " + x.data);
Console.Write( " is " + root.data + "\n" );
return null ;
}
}
return root;
}
return null ;
}
public static void inorderSuccessor(Node root, Node x)
{
if (x.right != null )
{
Node inorderSucc = leftMostNode(x.right);
Console.Write( "Inorder Successor of " +
x.data + " is " );
Console.Write(inorderSucc.data + "\n" );
}
if (x.right == null )
{
int f = 0;
Node rightMost = rightMostNode(root);
if (rightMost == x)
{
Console.Write( "No inorder successor! " +
"Right most node.\n" );
}
else
{
findInorderRecursive(root, x);
}
}
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(6);
inorderSuccessor(root, root.right);
inorderSuccessor(root, root.left.left);
inorderSuccessor(root, root.right.right);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data=data;
this .left= this .right= null ;
}
}
let temp = new Node();
function leftMostNode(node)
{
while (node != null && node.left != null )
node = node.left;
return node;
}
function rightMostNode(node)
{
while (node != null && node.right != null )
node = node.right;
return node;
}
function findInorderRecursive(root,x)
{
if (root== null )
return null ;
if (root==x || (temp = findInorderRecursive(root.left,x))!= null ||
(temp = findInorderRecursive(root.right,x))!= null )
{
if (temp!= null )
{
if (root.left == temp)
{
document.write( "Inorder Successor of " +x.data);
document.write( " is " + root.data + "<br>" );
return null ;
}
}
return root;
}
return null ;
}
function inorderSuccessor(root,x)
{
if (x.right != null )
{
let inorderSucc = leftMostNode(x.right);
document.write( "Inorder Successor of " +x.data+ " is " );
document.write(inorderSucc.data+ "<br>" );
}
if (x.right == null )
{
let f = 0;
let rightMost = rightMostNode(root);
if (rightMost == x)
document.write( "No inorder successor! Right most node.\n" );
else
findInorderRecursive(root, x);
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
inorderSuccessor(root, root.right);
inorderSuccessor(root, root.left.left);
inorderSuccessor(root, root.right.right);
</script>
|
Output:
Inorder Successor of 1 is 6
Inorder Successor of 4 is 2
No inorder successor! Right most node.
Time complexity: O(n) where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
Another approach:
We will do a reverse inorder traversal and keep the track of current visited node. Once we found the element, last tracked element would be our answer.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* left;
Node* right;
};
Node* newNode( int val)
{
Node* temp = new Node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void inorderSuccessor(Node* root,
Node* target_node,
Node* &next)
{
if (!root)
return ;
inorderSuccessor(root->right, target_node, next);
if (root->data == target_node->data)
{
if (next == NULL)
cout << "inorder successor of "
<< root->data << " is: null\n" ;
else
cout << "inorder successor of "
<< root->data << " is: "
<< next->data << "\n" ;
}
next = root;
inorderSuccessor(root->left, target_node, next);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
Node* next = NULL;
inorderSuccessor(root, root, next);
next = NULL;
inorderSuccessor(root, root->left->left, next);
next = NULL;
inorderSuccessor(root, root->right->right, next);
return 0;
}
|
Java
class Node {
int data;
Node left, right;
Node( int data) {
this .data = data;
left = null ; right = null ;
}
}
class InorderSuccessor {
Node root;
static class PreviousNode {
Node pNode;
PreviousNode() {
pNode = null ;
}
}
private void inOrderSuccessorOfBinaryTree(Node root,
PreviousNode pre, int searchNode)
{
if (root.right != null )
inOrderSuccessorOfBinaryTree(root.right, pre, searchNode);
if (root.data == searchNode)
System.out.println( "inorder successor of " + searchNode + " is: "
+ (pre.pNode != null ? pre.pNode.data : "null" ));
pre.pNode = root;
if (root.left != null )
inOrderSuccessorOfBinaryTree(root.left, pre, searchNode);
}
public static void main(String[] args)
{
InorderSuccessor tree = new InorderSuccessor();
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.left = new Node( 4 );
tree.root.left.right = new Node( 5 );
tree.root.right.right = new Node( 6 );
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 3 );
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 4 );
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 6 );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(val):
temp = Node( 0 )
temp.data = val
temp.left = None
temp.right = None
return temp
def inorderSuccessor(root, target_node):
global next
if (root = = None ):
return
inorderSuccessor(root.right, target_node)
if (root.data = = target_node.data):
if ( next = = None ):
print ( "inorder successor of" ,
root.data , " is: None" )
else :
print ( "inorder successor of" ,
root.data , "is:" , next .data)
next = root
inorderSuccessor(root.left, target_node)
next = None
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.right = newNode( 6 )
next = None
inorderSuccessor(root, root.right)
next = None
inorderSuccessor(root, root.left.left)
next = None
inorderSuccessor(root, root.right.right)
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = null ; right = null ;
}
}
public class InorderSuccessor
{
Node root;
class PreviousNode
{
public Node pNode;
public PreviousNode()
{
pNode = null ;
}
}
private void inOrderSuccessorOfBinaryTree(Node root,
PreviousNode pre, int searchNode)
{
if (root.right != null )
inOrderSuccessorOfBinaryTree(root.right,
pre, searchNode);
if (root.data == searchNode)
{
Console.Write( "inorder successor of " +
searchNode + " is: " );
if (pre.pNode != null )
Console.WriteLine(pre.pNode.data);
else
Console.WriteLine( "null" );
}
pre.pNode = root;
if (root.left != null )
inOrderSuccessorOfBinaryTree(root.left,
pre, searchNode);
}
public static void Main(String[] args)
{
InorderSuccessor tree = new InorderSuccessor();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.right = new Node(6);
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 3);
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 4);
tree.inOrderSuccessorOfBinaryTree(tree.root,
new PreviousNode(), 6);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
var root = null ;
class PreviousNode
{
constructor()
{
this .pNode = null ;
}
}
function inOrderSuccessorOfBinaryTree(root, pre, searchNode)
{
if (root.right != null )
inOrderSuccessorOfBinaryTree(root.right,
pre, searchNode);
if (root.data == searchNode)
{
document.write( "inorder successor of " +
searchNode + " is: " );
if (pre.pNode != null )
document.write(pre.pNode.data+ "<br>" );
else
document.write( "null<br>" );
}
pre.pNode = root;
if (root.left != null )
inOrderSuccessorOfBinaryTree(root.left,
pre, searchNode);
}
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
inOrderSuccessorOfBinaryTree(root,
new PreviousNode(), 3);
inOrderSuccessorOfBinaryTree(root,
new PreviousNode(), 4);
inOrderSuccessorOfBinaryTree(root,
new PreviousNode(), 6);
</script>
|
Output:
inorder successor of 1 is: 6
inorder successor of 4 is: 2
inorder successor of 7 is: null
Time Complexity: O( n ), where n is the number of nodes in the tree.
Space complexity: O(n) for call stack
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.