Populate Inorder Successor for all nodes | Python3 program to populate inorder traversal of all nodes ; A wrapper over populateNextRecur ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; Set next of all descendants of p by traversing them in reverse Inorder ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in right subtree
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 self . next = None NEW_LINE DEDENT DEDENT next = None NEW_LINE def populateNext ( node ) : NEW_LINE INDENT populateNextRecur ( node , next ) NEW_LINE DEDENT def populateNextRecur ( p , next_ref ) : NEW_LINE INDENT if ( p != None ) : NEW_LINE INDENT populateNextRecur ( p . right , next_ref ) NEW_LINE p . next = next_ref NEW_LINE next_ref = p NEW_LINE populateNextRecur ( p . left , next_ref ) NEW_LINE DEDENT DEDENT