From 343e2d8527889c019142eb6caa95b72a30be5371 Mon Sep 17 00:00:00 2001 From: Leo Ma Date: Sat, 28 Oct 2017 08:45:37 +0800 Subject: [PATCH 1/2] Convertion between bst and linked list Signed-off-by: Leo Ma --- .../Makefile | 2 + .../bst_convert.c | 41 ++++++++++ .../Makefile | 2 + .../flatten.c | 81 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 109_convert_sorted_list_to_binary_search_tree/Makefile create mode 100644 109_convert_sorted_list_to_binary_search_tree/bst_convert.c create mode 100644 114_flatten_binary_tree_to_linked_list/Makefile create mode 100644 114_flatten_binary_tree_to_linked_list/flatten.c diff --git a/109_convert_sorted_list_to_binary_search_tree/Makefile b/109_convert_sorted_list_to_binary_search_tree/Makefile new file mode 100644 index 0000000..113460f --- /dev/null +++ b/109_convert_sorted_list_to_binary_search_tree/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test bst_convert.c diff --git a/109_convert_sorted_list_to_binary_search_tree/bst_convert.c b/109_convert_sorted_list_to_binary_search_tree/bst_convert.c new file mode 100644 index 0000000..d5f5fa4 --- /dev/null +++ b/109_convert_sorted_list_to_binary_search_tree/bst_convert.c @@ -0,0 +1,41 @@ +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct TreeNode { + int val; + struct TreeNode *left; + struct TreeNode *right; +}; + +static struct TreeNode *recursive(int *nums, int lo, int hi) +{ + int mid = lo + (hi - lo) / 2; + struct TreeNode *node = malloc(sizeof(*node)); + node->val = nums[mid]; + node->left = mid > lo ? recursive(nums, lo, mid - 1) : NULL; + node->right = mid < hi ? recursive(nums, mid + 1, hi) : NULL; + return node; +} + +static struct TreeNode *sortedListToBST(struct ListNode *head) +{ + int i, nums[10000]; + for (i = 0; head != NULL; head = head->next, i++) { + nums[i] = head->val; + } + if (i == 0) { + return NULL; + } + return recursive(nums, 0, i - 1); +} + +int main(int argc, char **argv) +{ + sortedListToBST(NULL); + return 0; +} diff --git a/114_flatten_binary_tree_to_linked_list/Makefile b/114_flatten_binary_tree_to_linked_list/Makefile new file mode 100644 index 0000000..b4afb6d --- /dev/null +++ b/114_flatten_binary_tree_to_linked_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test flatten.c diff --git a/114_flatten_binary_tree_to_linked_list/flatten.c b/114_flatten_binary_tree_to_linked_list/flatten.c new file mode 100644 index 0000000..58072f6 --- /dev/null +++ b/114_flatten_binary_tree_to_linked_list/flatten.c @@ -0,0 +1,81 @@ +#include +#include +#include + +struct TreeNode { + int val; + struct TreeNode *left; + struct TreeNode *right; +}; + +static struct TreeNode *recursive(struct TreeNode *node) +{ + if (node == NULL) { + return NULL; + } + + if (node->right == NULL && node->left == NULL) { + return node; + } + + struct TreeNode *right_last = recursive(node->right); + struct TreeNode *left_last = recursive(node->left); + + if (left_last != NULL) { + left_last->right = node->right; + node->right = node->left; + node->left = NULL; + } + + return right_last != NULL ? right_last : left_last; +} + +static void flatten(struct TreeNode *root) +{ + recursive(root); +} + +int main(void) +{ + struct TreeNode root, n1[2], n2[4], n3[8]; + root.val = 5; + n1[0].val = 4; + n1[1].val = 8; + n2[0].val = 11; + n2[2].val = 13; + n2[3].val = 4; + n3[0].val = 7; + n3[1].val = 2; + n3[6].val = 5; + n3[7].val = 1; + + root.left = &n1[0]; + root.right = &n1[1]; + n1[0].left = &n2[0]; + n1[0].right = NULL; + n1[1].left = &n2[2]; + n1[1].right = &n2[3]; + n2[0].left = &n3[0]; + n2[0].right = &n3[1]; + n2[2].left = NULL; + n2[2].right = NULL; + n2[3].left = &n3[6]; + n2[3].right = &n3[7]; + n3[0].left = NULL; + n3[0].right = NULL; + n3[1].left = NULL; + n3[1].right = NULL; + n3[6].left = NULL; + n3[6].right = NULL; + n3[7].left = NULL; + n3[7].right = NULL; + + flatten(&root); + + struct TreeNode *p; + for (p = &root; p != NULL; p = p->right) { + printf("%d ", p->val); + } + printf("\n"); + return 0; +} From 4802005b2f9b8ce57172ca43a6750df7c1fa7668 Mon Sep 17 00:00:00 2001 From: Leo Ma Date: Sun, 29 Oct 2017 09:38:36 +0800 Subject: [PATCH 2/2] Reorder and insert list Signed-off-by: Leo Ma --- 143_reorder_list/Makefile | 2 + 143_reorder_list/reorder_list.c | 70 ++++++++++++++++++++++ 147_insertion_sort_list/Makefile | 2 + 147_insertion_sort_list/insert_sort_list.c | 61 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 143_reorder_list/Makefile create mode 100644 143_reorder_list/reorder_list.c create mode 100644 147_insertion_sort_list/Makefile create mode 100644 147_insertion_sort_list/insert_sort_list.c diff --git a/143_reorder_list/Makefile b/143_reorder_list/Makefile new file mode 100644 index 0000000..c6ca20b --- /dev/null +++ b/143_reorder_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test reorder_list.c diff --git a/143_reorder_list/reorder_list.c b/143_reorder_list/reorder_list.c new file mode 100644 index 0000000..a8d220a --- /dev/null +++ b/143_reorder_list/reorder_list.c @@ -0,0 +1,70 @@ +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +static void reverse(struct ListNode *dummy) +{ + struct ListNode *prev = dummy->next; + if (prev != NULL) { + struct ListNode *p = prev->next; + while (p != NULL) { + prev->next = p->next; + p->next = dummy->next; + dummy->next = p; + p = prev->next; + } + } +} + +static void reorderList(struct ListNode *head) +{ + if (head == NULL) { + return; + } + + int count = 0; + struct ListNode *p = head; + struct ListNode *q = p; + for (; p != NULL; p = p->next) { + if ((++count & 0x1) == 0) { + q = q->next; + } + } + + reverse(q); + + struct ListNode *r; + for (p = head, r = q->next; r != NULL; p = r->next, r = q->next) { + q->next = r->next; + r->next = p->next; + p->next = r; + } +} + +int main(int argc, char **argv) +{ + int i, count = argc - 1; + struct ListNode *head = NULL, *p, *prev; + for (i = 0; i < count; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + } else { + prev->next = p; + } + prev = p; + } + + reorderList(head); + for (p = head; p != NULL; p = p->next) { + printf("%d ", p->val); + } + printf("\n"); + return 0; +} diff --git a/147_insertion_sort_list/Makefile b/147_insertion_sort_list/Makefile new file mode 100644 index 0000000..576b707 --- /dev/null +++ b/147_insertion_sort_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test insert_sort_list.c diff --git a/147_insertion_sort_list/insert_sort_list.c b/147_insertion_sort_list/insert_sort_list.c new file mode 100644 index 0000000..bfae46c --- /dev/null +++ b/147_insertion_sort_list/insert_sort_list.c @@ -0,0 +1,61 @@ +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +static struct ListNode *insertionSortList(struct ListNode *head) +{ + if (head == NULL) { + return NULL; + } + + if (head->next == NULL) { + return head; + } + + struct ListNode dummy; + struct ListNode *p0, *p, *p1; + dummy.next = head; + + for (p0 = head, p = head->next; p != NULL; p0 = p, p = p->next) { + if (p->val < p0->val) { + p0->next = p->next; + for (p1 = &dummy; p1 != p0; p1 = p1->next) { + if (p1->next->val >= p->val) { + p->next = p1->next; + p1->next = p; + break; + } + } + p = p0; + } + } + + return dummy.next; +} + +int main(int argc, char **argv) +{ + int i, count = argc - 1; + struct ListNode *head = NULL, *p, *prev; + for (i = 0; i < count; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + } else { + prev->next = p; + } + prev = p; + } + + for (p = insertionSortList(head); p != NULL; p = p->next) { + printf("%d ", p->val); + } + printf("\n"); + return 0; +}