Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from begeekmyfriend:master #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 1 commit into AlgorithmAndLeetCode:master from begeekmyfriend:master
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions 0109_convert_sorted_list_to_binary_search_tree/bst_convert.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ struct TreeNode {
struct TreeNode *right;
};

static struct TreeNode *dfs(int *nums, int lo, int hi)
static struct TreeNode *dfs(struct ListNode **head, int lo, int hi)
{
if (lo > hi) {
return NULL;
}

int mid = lo + (hi - lo) / 2;
struct TreeNode *node = malloc(sizeof(*node));
node->val = nums[mid];
node->left = mid > lo ? dfs(nums, lo, mid - 1) : NULL;
node->right = mid < hi ? dfs(nums, mid + 1, hi) : NULL;
node->left = dfs(head, lo, mid - 1);
node->val = (*head)->val;
(*head) = (*head)->next;
node->right = dfs(head, mid + 1, hi);
return node;
}

static struct TreeNode *sortedListToBST(struct ListNode *head)
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;
struct ListNode *p;
int len = 0;
for (p = head; p != NULL; p = p->next) {
len++;
}
return traverse(nums, 0, i - 1);
return dfs(&head, 0, len - 1);
}

int main(int argc, char **argv)
Expand Down

AltStyle によって変換されたページ (->オリジナル) /