Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
begeekmyfriend committed Oct 30, 2017
2 parents f77f090 + 4802005 commit 06f42e8
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 109_convert_sorted_list_to_binary_search_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_convert.c
41 changes: 41 additions & 0 deletions 109_convert_sorted_list_to_binary_search_tree/bst_convert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
2 changes: 2 additions & 0 deletions 114_flatten_binary_tree_to_linked_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test flatten.c
81 changes: 81 additions & 0 deletions 114_flatten_binary_tree_to_linked_list/flatten.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
2 changes: 2 additions & 0 deletions 143_reorder_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test reorder_list.c
70 changes: 70 additions & 0 deletions 143_reorder_list/reorder_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
2 changes: 2 additions & 0 deletions 147_insertion_sort_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test insert_sort_list.c
61 changes: 61 additions & 0 deletions 147_insertion_sort_list/insert_sort_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>

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;
}

0 comments on commit 06f42e8

Please sign in to comment.