forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path108.c
31 lines (29 loc) · 726 Bytes
/
108.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode *convertBST(int *nums, int left, int right)
{
if (left > right)
return NULL;
else
{
int mid = (right + left) / 2;
struct TreeNode *new_val = malloc(sizeof(struct TreeNode));
new_val->val = nums[mid];
new_val->left = convertBST(nums, left, mid - 1);
new_val->right = convertBST(nums, mid + 1, right);
return new_val;
}
}
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
{
if (numsSize == 0)
return NULL;
else
return convertBST(nums, 0, numsSize - 1);
}