-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbt.cpp
85 lines (72 loc) · 1.69 KB
/
sbt.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
#include<iostream>
#define COUNT 10
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
node* newNode (int data)
{
node* temp = new node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void print2DUtil(node *root, int space)
{
if (root == NULL)
return;
space += COUNT;
print2DUtil(root->right, space);
cout<<endl;
for (int i = COUNT; i < space; i++)
cout<<" ";
cout<<"( "<<root->data<<" )\n";
print2DUtil(root->left, space);
}
node* constructTreeUtil (int pre[], int post[], int* preIndex, int l, int h, int size)
{
if (*preIndex >= size || l > h || pre[*preIndex]==0)
return NULL;
node* root = newNode ( pre[*preIndex] );
++*preIndex;
if (l == h)
return root;
int i;
for (i = l; i <= h; ++i)
if (pre[*preIndex] == post[i])
break;
if (i <= h)
{
root->left = constructTreeUtil (pre, post, preIndex, l, i, size);
root->right = constructTreeUtil (pre, post, preIndex, i + 1, h, size);
}
return root;
}
node *constructTree (int pre[], int post[], int size)
{
int preIndex = 0;
return constructTreeUtil (pre, post, &preIndex, 0, size - 1, size);
}
int main ()
{ int n;
int pre[50];
int post[50];
printf("Enter the number of nodes: ");
scanf("%d",&n);
printf("Enter the preorder traversal: ");
for(int i=0;i<n;i++)
scanf("%d",&pre[i]);
printf("Enter the postorder traversal: ");
for(int i=0;i<n;i++)
scanf("%d",&post[i]);
int size = sizeof( pre ) / sizeof( pre[0] );
node *root = constructTree(pre, post, size);
cout<<"The constructed tree: \n";
print2DUtil(root,0);
return 0;
}