题目:
代码(首刷自解 2024年1月31日):
class Solution {
public:
TreeNode* recursion(vector<int>& nums, int left, int right) {
if (left > right) return nullptr;
int mid = left + (right-left)/2;
TreeNode* node = new TreeNode(nums[mid]);
node->left = recursion(nums,left,mid - 1);
node->right = recursion(nums,mid + 1,right);
return node;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root = recursion(nums,0,nums.size()-1);
return root;
}
};