-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-traversal.c
55 lines (50 loc) · 1.15 KB
/
tree-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
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* l;
struct node* r;
};
struct node* NewNode(int val){
struct node* new = (struct node*)malloc(sizeof(struct node));
new->data = val;
new->l = NULL;
new->r = NULL;
return new;
}
void inorder(struct node* n){
if(n==NULL)
return;
inorder(n->l);
printf("%d\n",n->data);
inorder(n->r);
}
void postorder(struct node* n){
if(n==NULL)
return;
postorder(n->l);
postorder(n->r);
printf("%d\n",n->data);
}
void preorder(struct node* n){
if(n== NULL)
return;
printf("%d\n",n->data);
preorder(n->l);
preorder(n->r);
}
int main(){
struct node* root;
root = NewNode(1);
root->l = NewNode(2);
root->r = NewNode(3);
root->l->l = NewNode(4);
root->r->l = NewNode(5);
printf("Pre-order:\n");
preorder(root);
printf("In-order:\n");
inorder(root);
printf("Post-order:\n");
postorder(root);
return 0;
}