Inorder Successor of a node in Binary Tree | C ++ Program to find inorder successor . ; structure of a Binary Node . ; Function to create a new Node . ; function that prints the inorder successor of a target node . next will point the last tracked node , which will be the answer . ; if root is null then return ; if target node found then enter this condition ; Driver Code ; Let 's construct the binary tree   as shown in above diagram. ; Case 1 ; case 2 ; case 3
#include <bits/stdc++.h> NEW_LINE 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 STRNEWLINE " ; else cout << " inorder ▁ successor ▁ of ▁ " << root -> data << " ▁ is : ▁ " << next -> data << " STRNEWLINE " ; } 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 -> right = newNode ( 6 ) ; Node * next = NULL ; inorderSuccessor ( root , root -> right , next ) ; next = NULL ; inorderSuccessor ( root , root -> left -> left , next ) ; next = NULL ; inorderSuccessor ( root , root -> right -> right , next ) ; return 0 ; }