Preorder to Postorder

PROBLEM :
Given an array representing preorder traversal of BST, print its postorder traversal.

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input A[i].

Output:
Postorder traversal of the given preorder traversal is printed. Otherwise 'NO' is printed.

Constraints:
1 <=T<= 55
1 <= N <= 1000
1 <= arr[i] <= 1000

Example:

Input:
3
5
40 30 35 80 100
8
40 30 32 35 80 90 100 120
8
7  9 6 1 4 2 3 40

Output:
35 30 100 80 40
35 32 30 120 100 90 80 40
NO

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;

typedef struct TREE
{
    int data ;
    struct TREE *left ;
    struct TREE *right ;
}tree ;

int Preorder_Traversal_BST(int [],int ,int ) ;
int find_Rlarge(int [],int ,int ) ;
int allR_greater(int [],int ,int ,int ) ;
tree* Create_tree(int [],int ) ;
tree* insert_tree(tree *,int ) ;
void PostOrder_display(tree *) ;

int main()
 {
int t,no,arr[1000],i ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   if(!Preorder_Traversal_BST(arr,0,no-1))
       cout<<"NO" ;
   else
   {
       tree* root=NULL ;
       root=Create_tree(arr,no) ;
       PostOrder_display(root) ;
   }
     
   cout<<endl ;
}
return 0;
}

int Preorder_Traversal_BST(int arr[],int start,int end)
{
    int i ;
    if(start==end||start>end)
        return 1 ;
       
    int pos=find_Rlarge(arr,start,end) ;

       
    if(allR_greater(arr,pos,end,arr[start]))
    {
        if(Preorder_Traversal_BST(arr,start+1,pos-1)&&Preorder_Traversal_BST(arr,pos+1,end))
            return 1 ;
    }
    return 0 ;
}

int find_Rlarge(int arr[],int start,int end)
{
    int i ;
    for(i=start+1;i<=end;i++)
    {
        if(arr[i]>arr[start])
            return i ;
    }
    return end ;
}

int allR_greater(int arr[],int start,int end,int ele)
{
    int i ;
    for(i=start;i<=end;i++)
        if(arr[i]<ele)
            return 0 ;
           
    return 1 ;
}

tree* Create_tree(int arr[],int no)
{
    int i ;
    tree *root=NULL ;
   
    for(i=0;i<no;i++)
        root=insert_tree(root,arr[i]) ;
       
    return root ;
}

tree* insert_tree(tree *root,int ele)
{
    if(root==NULL)
    {
        tree *temp ;
        temp=(tree*)malloc(sizeof(tree)) ;
        temp->data=ele ;
        temp->left=NULL ;
        temp->right=NULL ;
        return temp ;
    }
   
    if(ele<=root->data)
         root->left=insert_tree(root->left,ele) ;
    else
         root->right=insert_tree(root->right,ele) ;
       
    return root ;
}

void PostOrder_display(tree *root)
{
    if(root==NULL)
        return ;
   
    PostOrder_display(root->left) ;
    PostOrder_display(root->right) ;
    cout<<root->data<<" " ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )