将有序数组转化成二叉搜索树
作者:互联网
详细思路
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); } };
标签:right,TreeNode,转化成,nums,int,dfs,二叉,数组,left 来源: https://www.cnblogs.com/zhouzihong/p/15087880.html