Print Postorder traversal from given Inorder and Preorder traversals | C # program to print postorder traversal from preorder and inorder 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
using System ; class GFG { static int search ( int [ ] arr , int x , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; } static void printPostOrder ( int [ ] in1 , int [ ] pre , int n ) { int root = search ( in1 , pre [ 0 ] , n ) ; int [ ] ar ; if ( root != 0 ) { ar = new int [ n - 1 ] ; Array . Copy ( pre , 1 , ar , 0 , n - 1 ) ; printPostOrder ( in1 , ar , root ) ; } if ( root != n - 1 ) { ar = new int [ n - ( root + 1 ) ] ; Array . Copy ( in1 , root + 1 , ar , 0 , n - ( root + 1 ) ) ; int [ ] ar1 = new int [ n - ( root + 1 ) ] ; Array . Copy ( pre , root + 1 , ar1 , 0 , n - ( root + 1 ) ) ; printPostOrder ( ar , ar1 , n - root - 1 ) ; } Console . Write ( pre [ 0 ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] in1 = { 4 , 2 , 5 , 1 , 3 , 6 } ; int [ ] pre = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = in1 . Length ; Console . WriteLine ( " Postorder ▁ traversal ▁ " ) ; printPostOrder ( in1 , pre , n ) ; } }