Find postorder traversal of BST from preorder traversal | C # 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
using System ; class GFG { public class INT { public int data ; public INT ( int d ) { data = d ; } } public static void findPostOrderUtil ( int [ ] pre , int n , int minval , int maxval , INT preIndex ) { if ( preIndex . data == n ) { return ; } if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } int val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; Console . Write ( val + " ▁ " ) ; } public static void findPostOrder ( int [ ] pre , int n ) { INT preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , int . MinValue , int . MaxValue , preIndex ) ; } public static void Main ( string [ ] args ) { int [ ] pre = new int [ ] { 40 , 30 , 35 , 80 , 100 } ; int n = pre . Length ; findPostOrder ( pre , n ) ; } }