-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinarytrees0.c
61 lines (56 loc) · 1.21 KB
/
binarytrees0.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
56
57
58
59
60
61
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int val){
struct node* n = (struct node*)malloc(sizeof(struct node));
n->data = val;
n->left = NULL;
n->right = NULL;
return n;
}
void preOrder(struct node* root){
if(root != NULL){
printf("%d->", root->data);
preOrder(root->left);
preOrder(root->right);
}
// printf("\n");
}
void postOrder(struct node* root){
if(root != NULL){
postOrder(root->left);
postOrder(root->right);
printf("%d->", root->data);
}
}
void inOrder(struct node* root){
if(root != NULL){
inOrder(root->left);
printf("%d->", root->data);
inOrder(root->right);
}
}
int main(){
struct node* p = createNode(5);
struct node* p1 = createNode(10);
struct node* p2 = createNode(15);
struct node* p3 = createNode(20);
struct node* p4 = createNode(25);
struct node* p5 = createNode(30);
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
p2->right = p5;
preOrder(p);
printf("\n");
postOrder(p);
printf("\n");
inOrder(p);
return 0;
}