-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArrayToBST.cpp
More file actions
29 lines (26 loc) · 876 Bytes
/
SortedArrayToBST.cpp
File metadata and controls
29 lines (26 loc) · 876 Bytes
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(num.size() == 0) return NULL; //add this to fix bug
int begin = 0;
int end = num.size()-1;
int middle = (begin+end)/2;
TreeNode* root = new TreeNode(num[middle]);
vector<int> left(num.begin(),num.begin()+middle);
root->left = sortedArrayToBST(left);
vector<int> right(num.begin()+middle+1,num.end());
root->right = sortedArrayToBST(right);
return root;
}
};