题干:
代码:
class Solution {
public:
TreeNode* traversal(vector<int>& nums, int left, int right){
if(left > right)return NULL;
int mid = left + (right - left)/2;
TreeNode* NewRoot = new TreeNode(nums[mid]);
NewRoot->left = traversal(nums, left, mid-1);
NewRoot->right = traversal(nums, mid+1,right);
return NewRoot;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
int left = 0;
int right = nums.size()-1;
return traversal(nums, left, right);
}
};
采取左闭右闭的区间划分方式。
一开始少了这一行导致了栈溢出。