-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalltraversals.cpp
More file actions
89 lines (77 loc) · 1.56 KB
/
alltraversals.cpp
File metadata and controls
89 lines (77 loc) · 1.56 KB
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
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
class BST
{
public:
int value;
BST*left;
BST*right;
BST(int value){
this->value = value;
left = nullptr;
right = nullptr;
}
};
void preorderTraversal(BST*tree,vector<int>&arr1)
{
if(tree == nullptr)
{
return;
}
arr1.push_back(tree->value);
preorderTraversal(tree->left,arr1);
preorderTraversal(tree->right);
}
void inorderTraversal(BST*tree,vector<int>&arr1)
{
if(tree == nullptr)
{
return;
}
inorderTraversal(tree->left,arr1);
arr1.push_back(tree->value);
inorderTraversal(tree->right);
}
void postorderTraversal(BST*tree,vector<int>&arr1)
{
if(tree == nullptr)
{
return;
}
postorderTraversal(tree->left,arr1);
postorderTraversal(tree->right);
arr1.push_back(tree->value);
}
int main()
{
BST*root = new BST(10);
root->left = new BST(5);
root->right = new BST(15);
root->left->left = new BST(2);
root->left->right = new BST(5);
root->right->left = new BST(13);
root->right->right = new BST(22);
root->left->left->left = new BST(1);
root->right->left->right = new BST(14);
vector<int>arr;
preorderTraversal(root,arr);
for(int i:arr)
{
cout<<i<<" ";
}
cout<<endl;
arr.clear();
inorderTraversal(root,arr);
for(int i:arr)
{
cout<<i<<" ";
}
cout<<endl;
arr.clear();
postorderTraversal(root,arr);
for(int i:arr)
{
cout<<i<<" ";
}
cout<<endl;
}