-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path108_c
30 lines (29 loc) · 920 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
int size = nums.size();
if(size < 1)
return NULL;
int mid = size / 2;
int i = 0;
TreeNode* tree = new TreeNode(nums[mid]);
TreeNode* out = tree;
for(i = mid-1;i >= 0;i--){
TreeNode* le = new TreeNode(nums[i]);
tree->left = le;
tree = le;
}
tree = out;
for(i = mid+1; i < size; i ++){
TreeNode* le = new TreeNode(nums[i]);
tree->right = le;
tree = le;
}
return out;
int size = nums.size();
if (size==0) return NULL;
int mid = size/2;
vector<int> left,right;
for(int i=0;i<mid;i++) left.push_back(nums[i]);
for(int i= mid+1;i<size;i++) right.push_back(nums[i]);
TreeNode *root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(left);
root->right = sortedArrayToBST(right);
return root;