-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPostorder, Preorder, Inorder.cpp
84 lines (65 loc) · 1.39 KB
/
Postorder, Preorder, Inorder.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
#include<bits/stdc++.h>
using namespace std;
void postorder(int a[], vector<int> &v, int n, int index)
{
if(index >= n)
return;
postorder(a, v, n, 2 * index + 1);
postorder(a, v, n, 2 * index + 2);
v.push_back(a[index]);
}
void inorder(int a[], vector<int> &v, int n, int index)
{
if(index >= n)
return;
inorder(a, v, n, 2 * index + 1);
v.push_back(a[index]);
inorder(a, v, n, 2 * index + 2);
}
void preorder(int a[], vector<int> &v, int n, int index)
{
if(index >= n)
return;
v.push_back(a[index]);
preorder(a, v, n, 2 * index + 1);
preorder(a, v, n, 2 * index + 2);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n=7;
int a[]={ 5, 6, 7, 8, 9, 10, 11};
cout<<"Original Series"<<endl;
for (int i = 0; i < n; ++i)
{
cout<<a[i]<<' ';
}
cout<<endl;
vector<int> v;
inorder(a, v, n, 0);
cout<<"INORDER :"<<'\n';
for (int i = 0; i < n; ++i)
{
cout<<v[i]<<' ';
}
v.clear();
cout<<endl;
cout<<"PREORDER :"<<'\n';
preorder(a, v, n, 0);
for (int i = 0; i < n; ++i)
{
cout<<v[i]<<' ';
}
v.clear();
cout<<endl;
cout<<"POSTORDER :"<<'\n';
postorder(a, v, n, 0);
for (int i = 0; i < n; ++i)
{
cout<<v[i]<<' ';
}
v.clear();
cout<<endl;
return 0;
}