-
Notifications
You must be signed in to change notification settings - Fork 0
/
108-arr2bst.c
49 lines (47 loc) · 1.17 KB
/
108-arr2bst.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* create(int val){
struct TreeNode* new = (struct TreeNode*)malloc(sizeof(struct TreeNode));
new->val = val;
new-> left = NULL;
new-> right = NULL;
return new;
}
struct TreeNode* insert(struct TreeNode* root, int val){
if(root==NULL){
return create(val);
}
if(root->val>val){
root = insert(root->left,val);
}
else if(root->val<val){
root = insert(root->right,val);
}
return root;
}
struct TreeNode* inorder(struct TreeNode* root, int* arr,int* count){
root=inorder(root->left,arr,count);
arr[*count++]=root->val;
root=inorder(root->right,arr,count);
return root;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
struct TreeNode* root;
int arr[numsSize];
int count=0;
for(int i=numsSize/2;i<numsSize;i++){
root = insert(root,nums[i]);
}
for(int i=(numsSize/2)-1;i>=0;i--){
root = insert(root,nums[i]);
}
struct TreeNode* root1;
inorder(root1,arr,&count);
return root;
}