-
Notifications
You must be signed in to change notification settings - Fork 0
/
Construct_BST_from_Preorder1.cpp
51 lines (51 loc) · 1.14 KB
/
Construct_BST_from_Preorder1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node *newnode(int data){
node *temp = new node();
temp->data = data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
node *construct_bst_util(int pre[], int &prei, int low, int high, int n){
if(prei>=n || low>high)
return NULL;
node *root = newnode(pre[prei]);
prei++;
if(low==high)
return root;
int i;
for(i=low;i<=high;i++){
if(pre[i] > root->data)
break;
}
root->left = construct_bst_util(pre, prei, prei, i-1, n);
root->right = construct_bst_util(pre, prei, i, high, n);
return root;
}
node *construct_bst(int pre[], int n){
int prei = 0;
return construct_bst_util(pre, prei, 0, n-1, n);
}
void inorder(node *root){
if(root==NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
int main(){
int n;
cin>>n;
int pre[n];
for(int i=0;i<n;i++)
cin>>pre[i];
node *root = construct_bst(pre,n);
inorder(root);
return 0;
}