-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108.cpp
More file actions
29 lines (28 loc) · 720 Bytes
/
Copy path108.cpp
File metadata and controls
29 lines (28 loc) · 720 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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* helper(const vector<int> &nums, int l, int r)
{
if(l > r)
return NULL;
int m = (l + r)/2;
TreeNode* root = new TreeNode(nums[m]);
root->left = helper(nums, l, m-1);
root->right = helper(nums, m+1, r);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
int len = nums.size();
if(len == 0) return NULL;
TreeNode* root = helper(nums, 0, len-1);
return root;
}
};