- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path102.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>
structTreeNode {
intval;
structTreeNode*left;
structTreeNode*right;
};
structQueueNode {
void*ptr;
structQueueNode*next;
};
structQueue{
structQueueNode*front;
structQueueNode*tail;
};
voidpush(structQueue*queue, void*new_val) {
structQueueNode*new_node= (structQueueNode*)malloc(sizeof(structQueueNode));
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(structQueue*queue) {
void*ans;
if (queue->front==NULL) {
ans=NULL;
}
else {
ans=queue->front->ptr;
structQueueNode*tmp=queue->front;
queue->front=queue->front->next;
if (queue->front==NULL) {
queue->tail=NULL;
}
free(tmp);
}
returnans;
}
/**
* 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(structTreeNode*root, int**columnSizes, int*returnSize) {
if (root==NULL) returnNULL;
#defineMAX 4096
int**ans= (int**)calloc(MAX, sizeof(int*));
*columnSizes= (int*)calloc(MAX, sizeof(int));
structQueueq;
q.front=q.tail=NULL;
push(&q, root);
ans[0] = (int*)calloc(1, sizeof(int)); /* for the root node */
*returnSize=0;
intnext=0; /* number of nodes in next level, except NULL nodes */
intcur=1; /* number of nodes in current level, except NULL nodes */
intcount=0; /* count of nodes already traversed in current level */
while (q.front!=NULL) {
structTreeNode*p= (structTreeNode*)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;
}
}
returnans;
}
intmain() {
structTreeNode*r= (structTreeNode*)calloc(7, sizeof(structTreeNode));
structTreeNode*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;
intreturnSize=0;
int**ret=levelOrder(r, &columnSizes, &returnSize);
inti, 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);
return0;
}