-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pre_In_Post_Order_In_1_Travesal.cpp
101 lines (85 loc) · 2.39 KB
/
Pre_In_Post_Order_In_1_Travesal.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
class Solution
{
public:
vector<vector<int>> PreInPost(Node *root)
{
stack<pair<Node *, int>> st;
vector<int> PreOrder, InOrder, PostOrder;
vector<vector<int>> ans;
if (root == NULL) return ans;
st.push({root, 1});
while (!st.empty())
{
auto it = st.top();
st.pop();
if (it.second == 1)
{
PreOrder.push_back(it.first->data);
it.second++;
st.push(it);
if (it.first->left != NULL) st.push({it.first->left, 1});
}
else if (it.second == 2)
{
InOrder.push_back(it.first->data);
it.second++;
st.push(it);
if (it.first->right != NULL) st.push({it.first->right, 1});
}
else
{
PostOrder.push_back(it.first->data);
}
}
ans.push_back(PreOrder);
ans.push_back(InOrder);
ans.push_back(PostOrder);
return ans;
}
};
int main()
{
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
Solution solution;
vector<vector<int>> result = solution.PreInPost(root);
cout << "Preorder: ";
for (int x : result[0]) {
cout << x << " ";
}
cout << endl;
cout << "Inorder: ";
for (int x : result[1]) {
cout << x << " ";
}
cout << endl;
cout << "Postorder: ";
for (int x : result[2]) {
cout << x << " ";
}
cout << endl;
delete root->left->left;
delete root->left->right;
delete root->right->left;
delete root->right->right;
delete root->left;
delete root->right;
delete root;
return 0;
}