forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
102.c
151 lines (116 loc) · 3.36 KB
/
102.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSize) {
if (root == NULL) return NULL;
#define MAX 4096
int **ans = (int **)calloc(MAX, sizeof(int *));
*columnSizes = (int *)calloc(MAX, sizeof(int));
struct Queue q;
q.front = q.tail = NULL;
push(&q, root);
ans[0] = (int *)calloc(1, sizeof(int)); /* for the root node */
*returnSize = 0;
int next = 0; /* number of nodes in next level, except NULL nodes */
int cur = 1; /* number of nodes in current level, except NULL nodes */
int count = 0; /* count of nodes already traversed in current level */
while (q.front != NULL) {
struct TreeNode *p = (struct TreeNode *)pop(&q);
if (p) { /* push children into queue if parent is not NULL */
ans[*returnSize][count] = p->val;
count++;
if (p->left) {
push(&q, p->left);
next++;
}
if (p->right) {
push(&q, p->right);
next++;
}
}
if (count == cur) {
(*columnSizes)[*returnSize] = cur;
(*returnSize)++;
ans[*returnSize] = (int *)calloc(next, sizeof(int)); /* for next level */
cur = next;
count = next = 0;
}
}
return ans;
}
int main() {
struct TreeNode *r = (struct TreeNode *)calloc(7, sizeof(struct TreeNode));
struct TreeNode *p = r;
p->val = 3;
p->left = r + 1;
p->right = r + 2;
p = r + 1;
p->val = 9;
p = r + 2;
p->val = 20;
p->left = r + 3;
p->right = r + 4;
p = r + 3;
p->val = 15;
p = r + 4;
p->val = 7;
int *columnSizes = NULL;
int returnSize = 0;
int **ret = levelOrder(r, &columnSizes, &returnSize);
int i, j;
for (i = 0; i < returnSize; i++) {
for (j = 0; j < columnSizes[i]; j++) {
printf("%d ", ret[i][j]);
}
printf("\n");
free(ret[i]);
}
free(ret);
free(columnSizes);
return 0;
}