forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0102-binary-tree-level-order-traversal.c
166 lines (136 loc) · 4.77 KB
/
0102-binary-tree-level-order-traversal.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#define ARRAY_ALLOCATION_SIZE 500
typedef struct TreeNode TreeNode;
// Circular queue with dynamic size
typedef struct Queue
{
TreeNode** treeNodeArray;
int treeNodeArraySize; // Allocated size of the treeNodeArray
int treeNodeArrayUsed; // Number of used space in the treeNodeArray
int front;
int back;
}Queue;
void queueInit(Queue* queue)
{
queue->treeNodeArray = (TreeNode**)malloc(ARRAY_ALLOCATION_SIZE * sizeof(TreeNode*));
queue->treeNodeArraySize = ARRAY_ALLOCATION_SIZE;
queue->treeNodeArrayUsed = 0;
queue->front = -1;
queue->back = -1;
}
int isQueueEmpty(Queue* queue)
{
if(queue->treeNodeArrayUsed == 0)
{
return true;
}
return false;
}
void queuePush(Queue* queue, TreeNode* node)
{
// Check if space reallocation is needed
if(queue->treeNodeArrayUsed == queue->treeNodeArraySize)
{
// Reallocate bigger space for the array
queue->treeNodeArraySize += ARRAY_ALLOCATION_SIZE; // Increase the array size
queue->treeNodeArray = (TreeNode**)realloc(queue->treeNodeArray, queue->treeNodeArraySize * sizeof(TreeNode*));
// If the front passed the back, we need to move any element statring of the front to the end of the array
if(queue->front >= queue->back)
{
int oldArraySize = (queue->treeNodeArraySize - ARRAY_ALLOCATION_SIZE);
// Calculate how many elements we need to move, starting from the from til the array end (with old array size before reallocation)
int elementsToMoveCount = oldArraySize - queue->front;
for(int i = 1; i < elementsToMoveCount; i++)
{
printf("%d, %d\n", i, elementsToMoveCount);
queue->treeNodeArray[queue->treeNodeArraySize - i] = queue->treeNodeArray[--oldArraySize];
}
// Set the new front position
queue->front = queue->treeNodeArraySize - elementsToMoveCount;
}
}
// If back is at the end of the array, we circle back to beginning of the array
if(++queue->back == queue->treeNodeArraySize)
{
queue->back = 0;
}
// Add node at the back
queue->treeNodeArray[queue->back] = node;
// Increment the treeNodeArrayUsed counter
queue->treeNodeArrayUsed++;
}
TreeNode* queuePop(Queue* queue)
{
// Make sure the queue is not empty
if(!isQueueEmpty(queue))
{
// Decrement the treeNodeArrayUsed counter
queue->treeNodeArrayUsed--;
// If front is at the end of the array, we circle back to beginning of the array
if(++queue->front == queue->treeNodeArraySize)
{
queue->front = 0;
}
return queue->treeNodeArray[queue->front];
}
return NULL;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
int resultAllocatedSize = ARRAY_ALLOCATION_SIZE;
int** result = (int**)malloc(resultAllocatedSize * sizeof(int*));
int resultIndex = 0;
Queue queue;
// Initialize to 0
*returnSize = 0;
*returnColumnSizes = (int*)malloc(resultAllocatedSize * sizeof(int));
if(!root)
{
return result;
}
queueInit(&queue);
// Initialize the queue with the root node
queuePush(&queue, root);
while(!isQueueEmpty(&queue))
{
int levelLen = queue.treeNodeArrayUsed;
int* level = (int*)malloc(levelLen * sizeof(int));
int levelIndex = 0;
for(int i = 0; i < levelLen; i++)
{
TreeNode* poppedNode = queuePop(&queue);
level[levelIndex++] = poppedNode->val;
if(poppedNode->left)
{
queuePush(&queue, poppedNode->left);
}
if(poppedNode->right)
{
queuePush(&queue, poppedNode->right);
}
}
// Check if result array has enough space
if(resultIndex + 1 == resultAllocatedSize)
{
resultAllocatedSize += ARRAY_ALLOCATION_SIZE;
result = (int**)realloc(result, resultAllocatedSize * sizeof(int*));
*returnColumnSizes = (int*)realloc(*returnColumnSizes, resultAllocatedSize * sizeof(int));
}
result[resultIndex] = level;
(*returnColumnSizes)[resultIndex] = levelLen;
resultIndex++;
(*returnSize)++;
}
return result;
}