-
Notifications
You must be signed in to change notification settings - Fork 0
/
postorderTraversal.c
53 lines (50 loc) · 1.16 KB
/
postorderTraversal.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
46
47
48
49
50
51
52
53
#include <stdio.h>
#include<limits.h>
#include<stdlib.h>
//declaration of the tree element right, left and data type
struct node
{
int data;
struct node *left;
struct node *right;
}*Node;
struct node *create()
{
int x;
//struct node *newnode;
struct node *newnode=(struct node*)malloc(sizeof(struct node));
printf ("Enter the data for the nodes (-1 for no data)");
scanf("%d",&x);
newnode->data=x;
if(x==-1)
{
printf ("Not entered any data\n");
return NULL;
}
printf("Enter the left child of the rooted data %d\t",newnode->data);
newnode->left=create();
printf("Enter the right child of the rooted data %d\t",newnode->data);
newnode->right=create();
return newnode;
}
//Postorder traversing using recursion
void postorder(struct node *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
printf ("%d ",root->data);
}
}
int main ()
{
//struct node *t,*root;
struct node *root=create();
struct node *t=root;
//traverse(t);
printf ("Root child : %d\n",t->data);
printf ("Postorder traversal\n");
postorder(t);
return 0;
}