-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostordertree.cpp
More file actions
57 lines (50 loc) · 837 Bytes
/
Copy pathpostordertree.cpp
File metadata and controls
57 lines (50 loc) · 837 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int value){
data = value;
left = nullptr;
right = nullptr;
}
};
// postorder
void print23 (Node* root){
if(root){
print23(root->left);
print23(root->right);
cout<<root->data<<" ";
}
}
// postorder
void postorder(Node* root){
stack<Node*>s;
stack<int>ss;
Node* temp;
s.push(root);
while(!s.empty()){
temp = s.top();
s.pop();
ss.push(temp->data);
if(temp->left)s.push(temp->left);
if(temp->right)s.push(temp->right);
}
while(!ss.empty()){
cout<<ss.top()<<" ";
ss.pop();
}
}
int main(){
Node* d = new Node(1);
d->left = new Node(2);
d->right = new Node(3);
d->left->right = new Node(5);
d->left->left = new Node(4);
postorder(d);
cout<<endl;
print23(d);
return 0;
}