|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#define BST_MAX_LEVEL 800 |
| 6 | + |
| 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)->prev; p != (head); p = p->prev) |
| 18 | + |
| 19 | +#define list_for_each_safe(p, n, head) \ |
| 20 | + for (p = (head)->prev, n = p->prev; p != (head); p = n, n = p->prev) |
| 21 | + |
| 22 | +struct TreeNode { |
| 23 | + int val; |
| 24 | + struct TreeNode *left; |
| 25 | + struct TreeNode *right; |
| 26 | +}; |
| 27 | + |
| 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) |
| 61 | +{ |
| 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 *count) |
| 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->right != NULL) { |
| 98 | + new = node_new(free_list, parent->node->right); |
| 99 | + list_add(&new->link, children); |
| 100 | + } |
| 101 | + if (parent->node->left != NULL) { |
| 102 | + new = node_new(free_list, parent->node->left); |
| 103 | + list_add(&new->link, children); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + int first = 1; |
| 108 | + list_for_each_safe(p, n, parents) { |
| 109 | + struct bfs_node *parent = list_entry(p, struct bfs_node, link); |
| 110 | + if (first) { |
| 111 | + first = 0; |
| 112 | + results[(*count)++] = parent->node->val; |
| 113 | + } |
| 114 | + list_del(p); |
| 115 | + list_add(p, free_list); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + ** Return an array of arrays of size *returnSize. |
| 121 | + ** The sizes of the arrays are returned as *columnSizes array. |
| 122 | + ** Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). |
| 123 | + **/ |
| 124 | +static int* rightSideView(struct TreeNode* root, int* returnSize) |
| 125 | +{ |
| 126 | + if (root == NULL) { |
| 127 | + *returnSize = 0; |
| 128 | + return NULL; |
| 129 | + } |
| 130 | + |
| 131 | + struct list_head q0; |
| 132 | + struct list_head q1; |
| 133 | + struct list_head free_list; |
| 134 | + INIT_LIST_HEAD(&q0); |
| 135 | + INIT_LIST_HEAD(&q1); |
| 136 | + INIT_LIST_HEAD(&free_list); |
| 137 | + |
| 138 | + int level = 0; |
| 139 | + *returnSize = 0; |
| 140 | + int *results = malloc(BST_MAX_LEVEL * sizeof(int)); |
| 141 | + struct bfs_node *new = node_new(&free_list, root); |
| 142 | + list_add_tail(&new->link, &q0); |
| 143 | + |
| 144 | + while (!list_empty(&q0) || !list_empty(&q1)) { |
| 145 | + if (level & 0x1) { |
| 146 | + queue(&q1, &q0, &free_list, results, returnSize); |
| 147 | + } else { |
| 148 | + queue(&q0, &q1, &free_list, results, returnSize); |
| 149 | + } |
| 150 | + level++; |
| 151 | + } |
| 152 | + |
| 153 | + return results; |
| 154 | +} |
| 155 | + |
| 156 | +int main(void) |
| 157 | +{ |
| 158 | + struct TreeNode root; |
| 159 | + root.val = 3; |
| 160 | + |
| 161 | + struct TreeNode node1[2]; |
| 162 | + node1[0].val = 9; |
| 163 | + node1[1].val = 20; |
| 164 | + |
| 165 | + struct TreeNode node2[4]; |
| 166 | + node2[2].val = 15; |
| 167 | + //node2[3].val = 7; |
| 168 | + |
| 169 | + root.left = &node1[0]; |
| 170 | + root.right = &node1[1]; |
| 171 | + |
| 172 | + node1[0].left = NULL; |
| 173 | + node1[0].right = NULL; |
| 174 | + node1[1].left = &node2[2]; |
| 175 | + node1[1].right = NULL;//&node2[3]; |
| 176 | + |
| 177 | + node2[0].left = NULL; |
| 178 | + node2[0].right = NULL; |
| 179 | + node2[1].left = NULL; |
| 180 | + node2[1].right = NULL; |
| 181 | + node2[2].left = NULL; |
| 182 | + node2[2].right = NULL; |
| 183 | + node2[3].left = NULL; |
| 184 | + node2[3].right = NULL; |
| 185 | + |
| 186 | + int i, count = 0; |
| 187 | + int *lists = rightSideView(&root, &count); |
| 188 | + for (i = 0; i < count; i++) { |
| 189 | + printf("%d ", lists[i]); |
| 190 | + } |
| 191 | + printf("\n"); |
| 192 | + |
| 193 | + return 0; |
| 194 | +} |
0 commit comments