-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102.binary-tree-level-order-traversal-iteration.c
145 lines (120 loc) · 3.37 KB
/
102.binary-tree-level-order-traversal-iteration.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
#include <stdlib.h>
#include <stdio.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define ElementType struct TreeNode *
#define ARRAY_SIZE 999
struct List
{
ElementType val;
struct List *next;
};
typedef struct List List;
List *newList(ElementType val)
{
List *list = (List *)malloc(sizeof(List));
list->val = val;
list->next = NULL;
return list;
}
List *addToList(List *list, ElementType val)
{
if (list == NULL)
{
return newList(val);
}
List *p = list;
while (p->next != NULL)
{
p = p->next;
}
p->next = newList(val);
return list;
}
List *popFromList(List *list)
{
List *newHead = list->next;
free(list);
return newHead;
}
/**
* 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)
{
*returnSize = 0;
int **result = (int **)malloc(sizeof(int *) * ARRAY_SIZE);
*returnColumnSizes = (int *)malloc(sizeof(int) * ARRAY_SIZE);
if (root == NULL)
{
return (int **)malloc(sizeof(int));
}
struct TreeNode *p = root;
List *list = newList(root);
while (list != NULL)
{
result[(*returnSize)] = (int *)malloc(sizeof(int) * ARRAY_SIZE);
(*returnColumnSizes)[(*returnSize)] = 0;
List *newLevelList = NULL;
while (list != NULL)
{
p = list->val;
result[(*returnSize)][((*returnColumnSizes)[(*returnSize)])++] = p->val;
list = popFromList(list);
if (p->left != NULL)
{
newLevelList = addToList(newLevelList, p->left);
}
if (p->right != NULL)
{
newLevelList = addToList(newLevelList, p->right);
}
}
list = newLevelList;
(*returnSize)++;
}
return result;
}
int main(int argc, char const *argv[])
{
struct TreeNode *rootRightRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightRight->val = 7;
rootRightRight->left = NULL;
rootRightRight->right = NULL;
struct TreeNode *rootRightLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeft->val = 15;
rootRightLeft->left = NULL;
rootRightLeft->right = NULL;
struct TreeNode *rootRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRight->val = 20;
rootRight->left = rootRightLeft;
rootRight->right = rootRightRight;
struct TreeNode *rootLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootLeft->val = 9;
rootLeft->left = NULL;
rootLeft->right = NULL;
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 3;
root->left = rootLeft;
root->right = rootRight;
List *list = newList(root);
int returnSize = 0;
int *returnColumnSizes = (int *)malloc(sizeof(int) * ARRAY_SIZE);
int **result = levelOrder(root, &returnSize, &returnColumnSizes);
for (size_t i = 0; i < returnSize; i++)
{
for (size_t j = 0; j < returnColumnSizes[i]; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}