Print Postorder traversal from given Inorder and Preorder traversals | A utility function to search x in arr [ ] of size n ; Prints postorder traversal from given inorder and preorder traversals ; The first element in pre [ ] is always root , search it in in [ ] to find left and right subtrees ; If left subtree is not empty , print left subtree ; If right subtree is not empty , print right subtree ; Print root ; Driver code
def search ( arr , x , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printPostOrder ( In , pre , n ) : NEW_LINE INDENT root = search ( In , pre [ 0 ] , n ) NEW_LINE if ( root != 0 ) : NEW_LINE INDENT printPostOrder ( In , pre [ 1 : n ] , root ) NEW_LINE DEDENT if ( root != n - 1 ) : NEW_LINE INDENT printPostOrder ( In [ root + 1 : n ] , pre [ root + 1 : n ] , n - root - 1 ) NEW_LINE DEDENT print ( pre [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT In = [ 4 , 2 , 5 , 1 , 3 , 6 ] NEW_LINE pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] NEW_LINE n = len ( In ) NEW_LINE print ( " Postorder ▁ traversal ▁ " ) NEW_LINE printPostOrder ( In , pre , n ) NEW_LINE