Skip to content

Commit

Permalink
Convertion between bst and linked list
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 28, 2017
1 parent ebc3bcf commit 343e2d8
Show file tree
Hide file tree
Showing 4 changed files with 126 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;
}

0 comments on commit 343e2d8

Please sign in to comment.