Skip to content

Commit 37f2e43

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent b7a7c35 commit 37f2e43

File tree

3 files changed

+50
-265
lines changed
  • 102_binary_tree_level_order_traversal
  • 103_binary_tree_zigzag_level_order_traversal
  • 107_binary_tree_level_order_traversal_ii

3 files changed

+50
-265
lines changed

102_binary_tree_level_order_traversal/bst_bfs.c

Lines changed: 18 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,154 +4,46 @@
44

55
#define BST_MAX_LEVEL 800
66

7-
#define container_of(ptr, type, member) \
8-
((type *)((char *)(ptr) - (size_t)&(((type *)0)->member)))
9-
10-
#define list_entry(ptr, type, member) \
11-
container_of(ptr, type, member)
12-
13-
#define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)
14-
#define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)
15-
16-
#define list_for_each(p, head) \
17-
for (p = (head)->next; p != (head); p = p->next)
18-
19-
#define list_for_each_safe(p, n, head) \
20-
for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
21-
227
struct TreeNode {
238
int val;
249
struct TreeNode *left;
2510
struct TreeNode *right;
2611
};
2712

28-
struct list_head {
29-
struct list_head *next, *prev;
30-
};
31-
32-
static inline void INIT_LIST_HEAD(struct list_head *list)
33-
{
34-
list->next = list->prev = list;
35-
}
36-
37-
static inline int list_empty(const struct list_head *head)
38-
{
39-
return (head->next == head);
40-
}
41-
42-
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
43-
{
44-
next->prev = new;
45-
new->next = next;
46-
new->prev = prev;
47-
prev->next = new;
48-
}
49-
50-
static inline void list_add(struct list_head *_new, struct list_head *head)
51-
{
52-
__list_add(_new, head, head->next);
53-
}
54-
55-
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
56-
{
57-
__list_add(_new, head->prev, head);
58-
}
59-
60-
static inline void __list_del(struct list_head *entry)
13+
static void bfs(struct TreeNode *root, int **results, int *count, int *col_sizes, int *size, int level)
6114
{
62-
entry->next->prev = entry->prev;
63-
entry->prev->next = entry->next;
64-
}
65-
66-
static inline void list_del(struct list_head *entry)
67-
{
68-
__list_del(entry);
69-
entry->next = entry->prev = NULL;
70-
}
71-
72-
struct bfs_node {
73-
struct TreeNode *node;
74-
struct list_head link;
75-
};
76-
77-
static struct bfs_node *node_new(struct list_head *free_list, struct TreeNode *node)
78-
{
79-
struct bfs_node *new;
80-
if (list_empty(free_list)) {
81-
new = malloc(sizeof(*new));
82-
} else {
83-
new = list_first_entry(free_list, struct bfs_node, link);
84-
list_del(&new->link);
85-
}
86-
new->node = node;
87-
return new;
88-
}
89-
90-
static void queue(struct list_head *parents, struct list_head *children,
91-
struct list_head *free_list, int **results, int *col_sizes, int level)
92-
{
93-
struct list_head *p, *n;
94-
list_for_each(p, parents) {
95-
struct bfs_node *new;
96-
struct bfs_node *parent = list_entry(p, struct bfs_node, link);
97-
if (parent->node->left != NULL) {
98-
new = node_new(free_list, parent->node->left);
99-
list_add_tail(&new->link, children);
100-
}
101-
if (parent->node->right != NULL) {
102-
new = node_new(free_list, parent->node->right);
103-
list_add_tail(&new->link, children);
104-
}
105-
col_sizes[level]++;
15+
if (root == NULL) {
16+
return;
10617
}
10718

108-
int i = 0;
109-
results[level] = malloc(col_sizes[level] * sizeof(int));
110-
list_for_each_safe(p, n, parents) {
111-
struct bfs_node *parent = list_entry(p, struct bfs_node, link);
112-
results[level][i++] = parent->node->val;
113-
list_del(p);
114-
list_add(p, free_list);
19+
*count = level + 1 > *count ? level + 1 : *count;
20+
if (col_sizes[level] == 0) {
21+
*size = *size > 256 ? 256 : *size * 2;
22+
results[level] = malloc(*size * sizeof(int));
11523
}
24+
results[level][col_sizes[level]++] = root->val;
25+
bfs(root->left, results, count, col_sizes, size, level + 1);
26+
bfs(root->right, results, count, col_sizes, size, level + 1);
11627
}
11728

11829
/**
11930
** Return an array of arrays of size *returnSize.
120-
** The sizes of the arrays are returned as *columnSizes array.
31+
** The sizes of the arrays are returned as *returnColumnSizes array.
12132
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
12233
**/
123-
static int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSize)
34+
static int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
12435
{
12536
if (root == NULL) {
12637
*returnSize = 0;
12738
return NULL;
12839
}
12940

130-
struct list_head free_list;
131-
struct list_head q0;
132-
struct list_head q1;
133-
INIT_LIST_HEAD(&free_list);
134-
INIT_LIST_HEAD(&q0);
135-
INIT_LIST_HEAD(&q1);
136-
41+
int size = 1;
42+
*returnSize = 0;
13743
int **results = malloc(BST_MAX_LEVEL * sizeof(int *));
138-
*columnSizes = malloc(BST_MAX_LEVEL * sizeof(int));
139-
memset(*columnSizes, 0, BST_MAX_LEVEL * sizeof(int));
140-
141-
int level = 0;
142-
struct bfs_node *new = node_new(&free_list, root);
143-
list_add_tail(&new->link, &q0);
144-
145-
while (!list_empty(&q0) || !list_empty(&q1)) {
146-
if (level & 0x1) {
147-
queue(&q1, &q0, &free_list, results, *columnSizes, level);
148-
} else {
149-
queue(&q0, &q1, &free_list, results, *columnSizes, level);
150-
}
151-
level++;
152-
}
153-
154-
*returnSize = level;
44+
*returnColumnSizes = malloc(BST_MAX_LEVEL * sizeof(int));
45+
memset(*returnColumnSizes, 0, BST_MAX_LEVEL * sizeof(int));
46+
bfs(root, results, returnSize, *returnColumnSizes, &size, 0);
15547
return results;
15648
}
15749

@@ -186,7 +78,7 @@ int main(void)
18678
node2[3].right = NULL;
18779

18880
int i, j, count = 0, *col_sizes;
189-
int **lists = levelOrder(&root, &col_sizes, &count);
81+
int **lists = levelOrder(&root, &count, &col_sizes);
19082
for (i = 0; i < count; i++) {
19183
for (j = 0; j < col_sizes[i]; j++) {
19284
printf("%d ", lists[i][j]);

103_binary_tree_zigzag_level_order_traversal/bst_zigzag.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ static void queue(struct list_head *parents, struct list_head *children, int rev
133133

134134
/**
135135
** Return an array of arrays of size *returnSize.
136-
** The sizes of the arrays are returned as *columnSizes array.
136+
** The sizes of the arrays are returned as *returnColumnSizes array.
137137
** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
138138
**/
139-
static int** zigzagLevelOrder(struct TreeNode* root, int** columnSizes, int* returnSize)
139+
static int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
140140
{
141141
if (root == NULL) {
142142
*returnSize = 0;
@@ -151,18 +151,18 @@ static int** zigzagLevelOrder(struct TreeNode* root, int** columnSizes, int* ret
151151
INIT_LIST_HEAD(&q1);
152152

153153
int **results = malloc(BST_MAX_LEVEL * sizeof(int *));
154-
*columnSizes = malloc(BST_MAX_LEVEL * sizeof(int));
155-
memset(*columnSizes, 0, BST_MAX_LEVEL * sizeof(int));
154+
*returnColumnSizes = malloc(BST_MAX_LEVEL * sizeof(int));
155+
memset(*returnColumnSizes, 0, BST_MAX_LEVEL * sizeof(int));
156156

157157
int level = 0;
158158
struct bfs_node *new = node_new(&free_list, root);
159159
list_add_tail(&new->link, &q0);
160160

161161
while (!list_empty(&q0) || !list_empty(&q1)) {
162162
if (level & 0x1) {
163-
queue(&q1, &q0, 1, &free_list, results, *columnSizes, level);
163+
queue(&q1, &q0, 1, &free_list, results, *returnColumnSizes, level);
164164
} else {
165-
queue(&q0, &q1, 0, &free_list, results, *columnSizes, level);
165+
queue(&q0, &q1, 0, &free_list, results, *returnColumnSizes, level);
166166
}
167167
level++;
168168
}
@@ -223,7 +223,7 @@ int main(void)
223223
node2[3].right = NULL;
224224

225225
int i, j, count = 0, *col_sizes;
226-
int **lists = zigzagLevelOrder(&root, &col_sizes, &count);
226+
int **lists = zigzagLevelOrder(&root, &count, &col_sizes);
227227
for (i = 0; i < count; i++) {
228228
for (j = 0; j < col_sizes[i]; j++) {
229229
printf("%d ", lists[i][j]);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy