-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathpostorder_traversal.cpp
55 lines (52 loc) · 1.14 KB
/
postorder_traversal.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
// Post-Order Traversal of a Binary-Tree
// Program Author : Abhisek Kumar Gupta
/*
40
/ \
10 30
/ \ / \
5 -1 -1 28
/ \ / \
1 -1 15 20
/ \ /\ /\
-1 -1 -1 -1 -1 -1
Input : 40 10 5 1 -1 -1 -1 -1 30 -1 28 15 -1 -1 20 -1 -1
Output : 1->5->10->15->20->28->30->40
*/
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int x){
data = x;
left = NULL;
right = NULL;
}
};
Node* build_binary_tree(){
int data;
cin >> data;
if(data == -1){
return NULL;
}
Node* root = new Node(data);
root->left = build_binary_tree();
root->right = build_binary_tree();
return root;
}
void print_binary_tree(Node* root){
if(root == NULL)
return;
print_binary_tree(root->left);
print_binary_tree(root->right);
cout << root->data << "->";
}
int main(){
Node* root = build_binary_tree();
print_binary_tree(root);
return 0;
}
//40 10 5 1 -1 -1 -1 -1 30 -1 28 15 -1 -1 20 -1 -1