/**
* 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* travel(vector<int>& nums, int left, int right) {
// 左闭右闭
if (left > right) return nullptr;
int mid = left + (right - left) / 2;
TreeNode* root = new TreeNode(0);
root->val = nums[mid];
root->left = travel(nums, left, mid - 1);
root->right = travel(nums, mid + 1, right);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return travel(nums, 0, nums.size() - 1);
}
};
06-23
402
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-05
94
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)