Print Postorder traversal from given Inorder and Preorder traversals | Javascript program to print Postorder traversal from given Inorder and Preorder traversals . ; Find index of next item in preorder traversal in inorder . ; Traverse left tree ; Traverse right tree ; Print root node at the end of traversal ; Driver code
let preIndex = 0 ; function printPost ( In , pre , inStrt , inEnd , hm ) { if ( inStrt > inEnd ) return ; let inIndex = hm . get ( pre [ preIndex ++ ] ) ; printPost ( In , pre , inStrt , inIndex - 1 , hm ) ; printPost ( In , pre , inIndex + 1 , inEnd , hm ) ; document . write ( In [ inIndex ] + " " ) ; } function printPostMain ( In , pre ) { let n = pre . length ; let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) hm . set ( In [ i ] , i ) ; printPost ( In , pre , 0 , n - 1 , hm ) ; } let In = [ 4 , 2 , 5 , 1 , 3 , 6 ] ; let pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] ; printPostMain ( In , pre ) ;