Inorder Successor of a node in Binary Tree | structure of a Binary Node . ; class to find inorder successor of a node ; to change previous node ; function to find inorder successor of a node ; Case1 : If right child is not NULL ; Case2 : If root data is equal to search node ; Driver program to test above functions ; Let 's construct the binary tree   as shown in above diagram  ; Case 1 ; Case 2 ; Case 3
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 ) ; } }