-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostorder_traversal.c
45 lines (41 loc) · 984 Bytes
/
postorder_traversal.c
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
#include <stdio.h>
#include <stdlib.h>
struct tree {
int val;
struct tree* left;
struct tree* right;
};
typedef struct tree TreeNode;
TreeNode* newTree(int data)
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = data;
root->left = NULL;
root->right = NULL;
return (root);
}
void postorder(TreeNode* root)
{
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->val);
}
int main()
{
TreeNode* t = newTree(9);
t->left = newTree(1);
t->left->left = newTree(10);
t->left->right = newTree(7);
t->left->right->left = newTree(2);
t->left->right->right = newTree(5);
t->left->right->right->left = newTree(0);
t->left->right->right->right = newTree(16);
t->right = newTree(19);
t->right->left = newTree(18);
t->right->right = newTree(20);
printf("postorder traversal of the above tree is:\n");
postorder(t);
return 0;
}