详细思路
dfs,形参leftright数据nums,生成一棵高度平衡的二叉搜索树,想要高度平衡可以将中点的值作为根结点 精确定义 dfs,形参leftright数据nums,生成高度平衡二叉搜索树,left>right返回空,最后返回class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { int n=nums.size(); return dfs(0,n-1,nums); } TreeNode*dfs(int left,int right,vector<int>&nums){ if(left>right)return nullptr; int mid=left+(right-left)/2; TreeNode*leftTree=dfs(left,mid-1,nums); TreeNode*rightTree=dfs(mid+1,right,numsa); return new TreeNode(nums[mid],leftTree,rightTree); } };