Print Postorder traversal from given Inorder and Preorder traversals | Python3 program to prPostorder traversal from given Inorder and Preorder traversals . ; Find index of next item in preorder traversal in inorder . ; traverse left tree ; traverse right tree ; prroot node at the end of traversal ; Driver code
def printPost ( inn , pre , inStrt , inEnd ) : NEW_LINE INDENT global preIndex , hm NEW_LINE if ( inStrt > inEnd ) : NEW_LINE INDENT return NEW_LINE DEDENT inIndex = hm [ pre [ preIndex ] ] NEW_LINE preIndex += 1 NEW_LINE printPost ( inn , pre , inStrt , inIndex - 1 ) NEW_LINE printPost ( inn , pre , inIndex + 1 , inEnd ) NEW_LINE print ( inn [ inIndex ] , end = " ▁ " ) NEW_LINE DEDENT def printPostMain ( inn , pre , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT hm [ inn [ i ] ] = i NEW_LINE DEDENT printPost ( inn , pre , 0 , n - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT hm = { } NEW_LINE preIndex = 0 NEW_LINE inn = [ 4 , 2 , 5 , 1 , 3 , 6 ] NEW_LINE pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] NEW_LINE n = len ( pre ) NEW_LINE printPostMain ( inn , pre , n ) NEW_LINE DEDENT