Repository navigation
Expand file tree
/
Copy pathbinary_tree.c
More file actions
69 lines (61 loc) · 1.38 KB
/
Copy pathbinary_tree.c
File metadata and controls
69 lines (61 loc) · 1.38 KB
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
62
63
64
65
66
67
68
69
# include <stdio.h>
# include <stdlib.h>
struct Node{
int data;
struct Node *left;
struct Node *right;
};
struct Node *createNode(int data){
struct Node *n=(struct Node *)malloc(sizeof(struct Node));
n->data=data;
n->left=NULL;
n->right=NULL;
return n;
}
void preorder_traversal(struct Node *root){
if (root!=NULL){
printf("%d ",root->data);
preorder_traversal(root->left);
preorder_traversal(root->right);
}
}
void postorder_traversal(struct Node *root){
if (root!=NULL){
postorder_traversal(root->left);
postorder_traversal(root->right);
printf("%d ",root->data);
}
}
void inorder_traversal(struct Node *root){
if (root!=NULL){
inorder_traversal(root->left);
printf("%d ",root->data);
inorder_traversal(root->right);
}
}
int main(){
struct Node *p=createNode(2);
struct Node *p2=createNode(1);
struct Node *p3=createNode(4);
struct Node *p4=createNode(11);
struct Node *p5=createNode(12);
struct Node *p6=createNode(3);
/* This is the tree
2
/\
1 4
/\ \
11 12 3
*/
p->left=p2;
p->right=p3;
p2->left=p4;
p2->right=p5;
p3->right=p6;
preorder_traversal(p);
printf("\n");
postorder_traversal(p);
printf("\n");
inorder_traversal(p);
return 0;
}