Skip to content

Commit 06f42e8

Browse files
2 parents f77f090 + 4802005 commit 06f42e8

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test bst_convert.c
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
struct TreeNode {
10+
int val;
11+
struct TreeNode *left;
12+
struct TreeNode *right;
13+
};
14+
15+
static struct TreeNode *recursive(int *nums, int lo, int hi)
16+
{
17+
int mid = lo + (hi - lo) / 2;
18+
struct TreeNode *node = malloc(sizeof(*node));
19+
node->val = nums[mid];
20+
node->left = mid > lo ? recursive(nums, lo, mid - 1) : NULL;
21+
node->right = mid < hi ? recursive(nums, mid + 1, hi) : NULL;
22+
return node;
23+
}
24+
25+
static struct TreeNode *sortedListToBST(struct ListNode *head)
26+
{
27+
int i, nums[10000];
28+
for (i = 0; head != NULL; head = head->next, i++) {
29+
nums[i] = head->val;
30+
}
31+
if (i == 0) {
32+
return NULL;
33+
}
34+
return recursive(nums, 0, i - 1);
35+
}
36+
37+
int main(int argc, char **argv)
38+
{
39+
sortedListToBST(NULL);
40+
return 0;
41+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test flatten.c
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
struct TreeNode {
6+
int val;
7+
struct TreeNode *left;
8+
struct TreeNode *right;
9+
};
10+
11+
static struct TreeNode *recursive(struct TreeNode *node)
12+
{
13+
if (node == NULL) {
14+
return NULL;
15+
}
16+
17+
if (node->right == NULL && node->left == NULL) {
18+
return node;
19+
}
20+
21+
struct TreeNode *right_last = recursive(node->right);
22+
struct TreeNode *left_last = recursive(node->left);
23+
24+
if (left_last != NULL) {
25+
left_last->right = node->right;
26+
node->right = node->left;
27+
node->left = NULL;
28+
}
29+
30+
return right_last != NULL ? right_last : left_last;
31+
}
32+
33+
static void flatten(struct TreeNode *root)
34+
{
35+
recursive(root);
36+
}
37+
38+
int main(void)
39+
{
40+
struct TreeNode root, n1[2], n2[4], n3[8];
41+
root.val = 5;
42+
n1[0].val = 4;
43+
n1[1].val = 8;
44+
n2[0].val = 11;
45+
n2[2].val = 13;
46+
n2[3].val = 4;
47+
n3[0].val = 7;
48+
n3[1].val = 2;
49+
n3[6].val = 5;
50+
n3[7].val = 1;
51+
52+
root.left = &n1[0];
53+
root.right = &n1[1];
54+
n1[0].left = &n2[0];
55+
n1[0].right = NULL;
56+
n1[1].left = &n2[2];
57+
n1[1].right = &n2[3];
58+
n2[0].left = &n3[0];
59+
n2[0].right = &n3[1];
60+
n2[2].left = NULL;
61+
n2[2].right = NULL;
62+
n2[3].left = &n3[6];
63+
n2[3].right = &n3[7];
64+
n3[0].left = NULL;
65+
n3[0].right = NULL;
66+
n3[1].left = NULL;
67+
n3[1].right = NULL;
68+
n3[6].left = NULL;
69+
n3[6].right = NULL;
70+
n3[7].left = NULL;
71+
n3[7].right = NULL;
72+
73+
flatten(&root);
74+
75+
struct TreeNode *p;
76+
for (p = &root; p != NULL; p = p->right) {
77+
printf("%d ", p->val);
78+
}
79+
printf("\n");
80+
return 0;
81+
}

143_reorder_list/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test reorder_list.c

143_reorder_list/reorder_list.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
static void reverse(struct ListNode *dummy)
10+
{
11+
struct ListNode *prev = dummy->next;
12+
if (prev != NULL) {
13+
struct ListNode *p = prev->next;
14+
while (p != NULL) {
15+
prev->next = p->next;
16+
p->next = dummy->next;
17+
dummy->next = p;
18+
p = prev->next;
19+
}
20+
}
21+
}
22+
23+
static void reorderList(struct ListNode *head)
24+
{
25+
if (head == NULL) {
26+
return;
27+
}
28+
29+
int count = 0;
30+
struct ListNode *p = head;
31+
struct ListNode *q = p;
32+
for (; p != NULL; p = p->next) {
33+
if ((++count & 0x1) == 0) {
34+
q = q->next;
35+
}
36+
}
37+
38+
reverse(q);
39+
40+
struct ListNode *r;
41+
for (p = head, r = q->next; r != NULL; p = r->next, r = q->next) {
42+
q->next = r->next;
43+
r->next = p->next;
44+
p->next = r;
45+
}
46+
}
47+
48+
int main(int argc, char **argv)
49+
{
50+
int i, count = argc - 1;
51+
struct ListNode *head = NULL, *p, *prev;
52+
for (i = 0; i < count; i++) {
53+
p = malloc(sizeof(*p));
54+
p->val = atoi(argv[i + 1]);
55+
p->next = NULL;
56+
if (head == NULL) {
57+
head = p;
58+
} else {
59+
prev->next = p;
60+
}
61+
prev = p;
62+
}
63+
64+
reorderList(head);
65+
for (p = head; p != NULL; p = p->next) {
66+
printf("%d ", p->val);
67+
}
68+
printf("\n");
69+
return 0;
70+
}

147_insertion_sort_list/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test insert_sort_list.c
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
static struct ListNode *insertionSortList(struct ListNode *head)
10+
{
11+
if (head == NULL) {
12+
return NULL;
13+
}
14+
15+
if (head->next == NULL) {
16+
return head;
17+
}
18+
19+
struct ListNode dummy;
20+
struct ListNode *p0, *p, *p1;
21+
dummy.next = head;
22+
23+
for (p0 = head, p = head->next; p != NULL; p0 = p, p = p->next) {
24+
if (p->val < p0->val) {
25+
p0->next = p->next;
26+
for (p1 = &dummy; p1 != p0; p1 = p1->next) {
27+
if (p1->next->val >= p->val) {
28+
p->next = p1->next;
29+
p1->next = p;
30+
break;
31+
}
32+
}
33+
p = p0;
34+
}
35+
}
36+
37+
return dummy.next;
38+
}
39+
40+
int main(int argc, char **argv)
41+
{
42+
int i, count = argc - 1;
43+
struct ListNode *head = NULL, *p, *prev;
44+
for (i = 0; i < count; i++) {
45+
p = malloc(sizeof(*p));
46+
p->val = atoi(argv[i + 1]);
47+
p->next = NULL;
48+
if (head == NULL) {
49+
head = p;
50+
} else {
51+
prev->next = p;
52+
}
53+
prev = p;
54+
}
55+
56+
for (p = insertionSortList(head); p != NULL; p = p->next) {
57+
printf("%d ", p->val);
58+
}
59+
printf("\n");
60+
return 0;
61+
}

0 commit comments

Comments
 (0)