Skip to content

Commit 343e2d8

Browse files
Convertion between bst and linked list
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent ebc3bcf commit 343e2d8

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-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+
}

0 commit comments

Comments
 (0)