(1)递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
if(nums.size()==0) return nullptr;
int start=0,end=nums.size()-1;
int mid=(start+end)/2;
TreeNode *root=new TreeNode(nums[mid]);
vector<int> l,r;
for(int i=0;i<nums.size();i++) {
if(i<mid) l.push_back(nums[i]);
else if(i>mid) r.push_back(nums[i]);
}
root->left=sortedArrayToBST(l);
root->right=sortedArrayToBST(r);
return root;
}
};