-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_145_PostOrderTraversalBT.cpp
288 lines (230 loc) · 6.15 KB
/
LC_145_PostOrderTraversalBT.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
https://leetcode.com/problems/binary-tree-postorder-traversal/
https://practice.geeksforgeeks.org/problems/postorder-traversal-iterative/0/#
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal_1(TreeNode* root) {
stack<TreeNode*> st;
vector<int> ans;
TreeNode* pre=nullptr, *cur = root;
while(cur || !st.empty())
{
if(cur != nullptr)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
if(cur->right == nullptr || cur->right == pre)
{
ans.push_back(cur->val);
st.pop();
pre = cur;
cur = nullptr;
}
else
cur = cur->right;
}
}//while
return ans;
}
vector<int> postorderTraversal(TreeNode* root) {
if(!root)
return {};
stack<TreeNode*> st;
vector<int> ans;
st.push(root);
while(!st.empty())
{
TreeNode* t = st.top();
if(t->left)
{
st.push(t->left);
t->left=nullptr;
continue;
}
if(t->right)
{
st.push(t->right);
t->right = nullptr;
continue;
}
ans.push_back(t->val);
st.pop();
}
return ans;
}
};
//GFG_ -------------------------------------------
// { Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
#define MAX_HEIGHT 100000
// Tree Node
struct Node {
int data;
Node* left;
Node* right;
};
// Utility function to create a new Tree Node
Node* newNode(int val) {
Node* temp = new Node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// Function to Build Tree
Node* buildTree(string str) {
// Corner Case
if (str.length() == 0 || str[0] == 'N') return NULL;
// Creating vector of strings from input
// string after spliting by space
vector<string> ip;
istringstream iss(str);
for (string str; iss >> str;) ip.push_back(str);
// Create the root of the tree
Node* root = newNode(stoi(ip[0]));
// Push the root to the queue
queue<Node*> queue;
queue.push(root);
// Starting from the second element
int i = 1;
while (!queue.empty() && i < ip.size()) {
// Get and remove the front of the queue
Node* currNode = queue.front();
queue.pop();
// Get the current node's value from the string
string currVal = ip[i];
// If the left child is not null
if (currVal != "N") {
// Create the left child for the current node
currNode->left = newNode(stoi(currVal));
// Push it to the queue
queue.push(currNode->left);
}
// For the right child
i++;
if (i >= ip.size()) break;
currVal = ip[i];
// If the right child is not null
if (currVal != "N") {
// Create the right child for the current node
currNode->right = newNode(stoi(currVal));
// Push it to the queue
queue.push(currNode->right);
}
i++;
}
return root;
}
// } Driver Code Ends
// User function Template for C++
/* Tree Node
struct Node {
int data;
Node* left;
Node* right;
};*/
class Solution{
public:
vector<int> postOrder(Node* node) {
// return postOrderReverse(node);
// return postOrderReverseUsingStack(node);
stack<Node*> st;
vector<int> ans;
Node* pre=nullptr, *cur = node;
while(cur || !st.empty())
{
if(cur != nullptr)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
if(cur->right == nullptr || cur->right == pre)
{
ans.push_back(cur->data);
st.pop();
pre = cur;
cur = nullptr;
}
else
cur = cur->right;
}
}//while
return ans;
}
vector<int> postOrderReverseUsingStack(Node* node) {
stack<Node*> st1, st2;
vector<int> ans;
st1.push(node);
while(!st1.empty())
{
Node* t = st1.top(); st1.pop();
st2.push(t);
if(t->left)
st1.push(t->left);
if(t->right)
st1.push(t->right);
}
while(!st2.empty())
{
ans.push_back(st2.top()->data);
st2.pop();
}
return ans;
}
vector<int> postOrderReverse(Node* node) {
stack<Node*> st;
vector<int> ans;
st.push(node);
while(!st.empty())
{
Node* t = st.top(); st.pop();
ans.push_back(t->data);
if(t->left)
st.push(t->left);
if(t->right)
st.push(t->right);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
// { Driver Code Starts.
/* Driver program to test size function*/
int main() {
int t;
scanf("%d ", &t);
while (t--) {
string s, ch;
getline(cin, s);
Node* root = buildTree(s);
vector<int> ans;
Solution ob;
ans = ob.postOrder(root) ;
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends