/**
* 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 NULL;
if(nums.size()==1)return new TreeNode(nums[0]);
int mid = nums.size()/2;
vector<int> l = vector<int>(nums.begin(),nums.begin()+mid);
vector<int> r = vector<int>(nums.begin()+mid+1,nums.end());
TreeNode* root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(l);
root->right = sortedArrayToBST(r);
return root;
}
};
标签:right,TreeNode,nums,int,随想录,二叉,vector,数组,left
From: https://www.cnblogs.com/huigugu/p/18680456