/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return TreeNode类
*/
TreeNode*create_BST(vector<int>&nums,int left,int right){
if(left>right){
return NULL;
}
int mid=(left+right+1)/2;
TreeNode *root=new TreeNode(nums[mid]);
root->left=create_BST(nums,left,mid-1);
root->right=create_BST(nums,mid+1,right);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
// write code here
if(nums.size()==0){
return NULL;
}
return create_BST(nums,0,nums.size()-1);
}
};