Postorder traversal of Binary Tree without recursion and without stack | A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Visited left subtree ; Visited right subtree ; Print node ; Driver program to test above functions
class newNode : 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 postorder ( head ) : NEW_LINE INDENT temp = head NEW_LINE visited = set ( ) NEW_LINE while ( temp and temp not in visited ) : NEW_LINE INDENT if ( temp . left and temp . left not in visited ) : NEW_LINE INDENT temp = temp . left NEW_LINE DEDENT elif ( temp . right and temp . right not in visited ) : NEW_LINE INDENT temp = temp . right NEW_LINE DEDENT else : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE visited . add ( temp ) NEW_LINE temp = head NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE postorder ( root ) NEW_LINE DEDENT