Find postorder traversal of BST from preorder traversal | Javascript program for finding postorder traversal of BST from preorder traversal ; Function to find postorder traversal from preorder traversal . ; If entire preorder array is traversed then return as no more element is left to be added to post order array . ; If array element does not lie in range specified , then it is not part of current subtree . ; Store current value , to be printed later , after printing left and right subtrees . Increment preIndex to find left and right subtrees , and pass this updated value to recursive calls . ; All elements with value between minval and val lie in left subtree . ; All elements with value between val and maxval lie in right subtree . ; Function to find postorder traversal . ; To store index of element to be traversed next in preorder array . This is passed by reference to utility function . ; Driver code ; Calling function
class INT { constructor ( d ) { this . data = d ; } } function findPostOrderUtil ( pre , n , minval , maxval , preIndex ) { if ( preIndex . data == n ) return ; if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } let val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; document . write ( val + " " ) ; } function findPostOrder ( pre , n ) { let preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , Number . MIN_VALUE , Number . MAX_VALUE , preIndex ) ; } let pre = [ 40 , 30 , 35 , 80 , 100 ] ; let n = pre . length ; findPostOrder ( pre , n ) ;