-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8binary_tree.c
64 lines (56 loc) · 1.39 KB
/
8binary_tree.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
62
63
64
/*************************************************************************
> File Name: 8binary_tree.c
> Author: snowflake
> Mail: ︿( ̄︶ ̄)︿
> Created Time: 2018年09月14日 星期五 15时31分18秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left, *right;
} Node;
Node *get_node(int data) {
Node *p = (Node *)calloc(sizeof(Node), 1);
p->data = data;
return p;
}
void preorder(Node *root) {
if (root == NULL) return ;
printf("%d ", root->data);
preorder(root->left);
printf("left\n");
preorder(root->right);
printf("right\n");
return ;
}
void inorder(Node *root) {
if (root == NULL) return ;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void postorder(Node *root) {
if (root == NULL) return ;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
Node *insert_node(Node *root) {
if (root == NULL) root = get_node(1);
root->left = get_node(2);
root->right = get_node(3);
root->left->left = get_node(4);
root->right->right = get_node(5);
return root;
}
int main() {
Node *root = NULL;
root = insert_node(root);
preorder(root);
printf("\n");
inorder(root);
printf("\n");
postorder(root);
return 0;
}