Inorder Successor of a node in Binary Tree | A Binary Tree 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 None 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
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( val ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = val NEW_LINE temp . left = None NEW_LINE temp . right = None NEW_LINE return temp NEW_LINE DEDENT def inorderSuccessor ( root , target_node ) : NEW_LINE INDENT global next NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorderSuccessor ( root . right , target_node ) NEW_LINE if ( root . data == target_node . data ) : NEW_LINE INDENT if ( next == None ) : NEW_LINE INDENT print ( " inorder ▁ successor ▁ of " , root . data , " ▁ is : ▁ None " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " inorder ▁ successor ▁ of " , root . data , " is : " , next . data ) NEW_LINE DEDENT DEDENT next = root NEW_LINE inorderSuccessor ( root . left , target_node ) NEW_LINE DEDENT next = None NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE next = None NEW_LINE inorderSuccessor ( root , root . right ) NEW_LINE next = None NEW_LINE inorderSuccessor ( root , root . left . left ) NEW_LINE next = None NEW_LINE inorderSuccessor ( root , root . right . right ) NEW_LINE DEDENT