forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convertion between bst and linked list
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
ebc3bcf
commit 343e2d8
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
109_convert_sorted_list_to_binary_search_tree/bst_convert.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test flatten.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |