Skip to content

Commit acf802a

Browse files
Binary Tree Depth Traversal -> Post-Order Done
1 parent 9aed1b2 commit acf802a

3 files changed

+56
-1
lines changed

binary_tree_postorder_traversal.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *left;
9+
Node *right;
10+
Node(int val)
11+
{
12+
this->val = val;
13+
this->left = NULL;
14+
this->right = NULL;
15+
}
16+
};
17+
18+
void postorder(Node *root)
19+
{
20+
if (root == NULL)
21+
return;
22+
postorder(root->left);
23+
postorder(root->right);
24+
cout << root->val << " ";
25+
}
26+
27+
int main()
28+
{
29+
Node *root = new Node(10);
30+
Node *a = new Node(20);
31+
Node *b = new Node(30);
32+
Node *c = new Node(40);
33+
Node *d = new Node(50);
34+
Node *e = new Node(60);
35+
Node *f = new Node(70);
36+
Node *g = new Node(80);
37+
Node *h = new Node(90);
38+
Node *i = new Node(100);
39+
40+
// Connections
41+
root->left = a;
42+
root->right = b;
43+
a->left = c;
44+
a->right = h;
45+
c->right = e;
46+
b->right = d;
47+
d->left = f;
48+
d->right = g;
49+
h->right = i;
50+
51+
// Print using Post-Order Traversal
52+
postorder(root);
53+
54+
return 0;
55+
}

binary_tree_postorder_traversal.exe

50.2 KB
Binary file not shown.

output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10 20 40 60 90 100 30 50 70 80
1+
60 40 100 90 20 70 80 50 30 10

0 commit comments

Comments
 (0)