-
Notifications
You must be signed in to change notification settings - Fork 118
/
108.c
102 lines (78 loc) · 2.16 KB
/
108.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct QueueNode {
void * ptr;
struct QueueNode *next;
};
struct Queue{
struct QueueNode *front;
struct QueueNode *tail;
};
void push(struct Queue *queue, void *new_val) {
struct QueueNode *new_node = (struct QueueNode *)malloc(sizeof(struct QueueNode));
new_node->ptr = new_val;
new_node->next = NULL;
if (queue->tail != NULL) {
queue->tail->next = new_node;
}
queue->tail = new_node;
if (queue->front == NULL) {
queue->front = new_node;
}
}
void * pop(struct Queue * queue) {
void *ans;
if (queue->front == NULL) {
ans = NULL;
}
else {
ans = queue->front->ptr;
struct QueueNode *tmp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->tail = NULL;
}
free(tmp);
}
return ans;
}
void printTreeLevelOrder(struct TreeNode *root) {
struct Queue q;
q.front = q.tail = NULL;
push(&q, root);
while (q.front != NULL) {
struct TreeNode * p = (struct TreeNode *)pop(&q);
if (p) {
printf("%d", p->val);
push(&q, p->left);
push(&q, p->right);
}
else {
printf("#");
}
}
printf("\n");
}
/* recursive */
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
if (nums == NULL || numsSize == 0) return NULL;
int middle = numsSize / 2; /* when numsSize is even, it's the right one */
struct TreeNode *root
= (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = nums[middle];
root->left = sortedArrayToBST(nums, middle);
root->right = sortedArrayToBST(nums + middle + 1, numsSize - middle - 1);
return root;
}
int main () {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
struct TreeNode *r = sortedArrayToBST(nums, sizeof(nums) / sizeof(nums[0]));
/* 53824791###6### */
printTreeLevelOrder(r);
return 0;
}