Preorder Traversal and BST
PROBLEM :
Given an array, write a program that prints 1 if given array can represent preorder traversal of a BST, else prints 0.
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:
Should print 1 if given array can represent preorder traversal of BST. Otherwise 0.
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:
1
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Preorder_Traversal_BST(int [],int ,int ) ;
int find_Rlarge(int [],int ,int ) ;
int allR_greater(int [],int ,int ,int ) ;
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<<1 ;
else
cout<<0 ;
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 ;
}
---------------------------------------------------------------------------------
Given an array, write a program that prints 1 if given array can represent preorder traversal of a BST, else prints 0.
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:
Should print 1 if given array can represent preorder traversal of BST. Otherwise 0.
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:
1
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Preorder_Traversal_BST(int [],int ,int ) ;
int find_Rlarge(int [],int ,int ) ;
int allR_greater(int [],int ,int ,int ) ;
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<<1 ;
else
cout<<0 ;
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 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment