Preorder to Postorder Java

PROGRAM TO FIND POSTORDER TRAVERSAL OF BST FROM PREORDER TRAVERSAL



import java.io.*;
 
class MAIN {
     
    public void getPostOrderBST(int pre[])
    {
        int pivotPoint = 0;
       
        // run loop from 1 to length of pre
        for (int i = 1; i < pre.length; i++)
        {
            if (pre[0] <= pre[i])
            {
                pivotPoint = i;
                break;
            }
        }
       
        // print from pivot length -1 to zero
        for (int i = pivotPoint - 1; i > 0; i--)
        {
            System.out.print(pre[i] + " ");
        }
       
        // print from end to pivot length
        for (int i = pre.length - 1; i >= pivotPoint; i--)
        {
            System.out.print(pre[i] + " ");
        }
        System.out.print(pre[0]);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        GFG obj = new GFG();
        int pre[] = { 40, 30, 32, 35, 80, 90, 100, 120 };
        obj.getPostOrderBST(pre);
    }
}


OUTPUT:
35 32 30 120 100 90 80 40

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java