-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.cpp
49 lines (42 loc) · 886 Bytes
/
main.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
/* you only have to complete the function given below.
Node is defined as
struct node
{
int data;
node* left;
node* right;
};
*/
#include <iostream>
using namespace std;
// Recursive approach
void Postorder(node *root) {
if (root != NULL) {
Postorder(root->left);
Postorder(root->right);
cout << root->data << " ";
}
}
#include <stack>
// Iterative approach
void Postorder(node *root) {
stack<node*> s1, s2;
s1.push(root);
node* current;
while (!s1.empty()) {
current = s1.top();
s1.pop();
if (current != NULL) {
s2.push(current);
s1.push(current->left);
s1.push(current->right);
}
}
while (!s2.empty()) {
current = s2.top();
s2.pop();
if (current != NULL) {
cout << current->data << " ";
}
}
}