/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num)
{
if(num.size()==0)
return NULL;
return Convert(num,0,num.size()-1);
}
TreeNode* Convert(vector<int> &num,int start,int end)
{
if(start<=end)
{
int index=start+(end-start)/2;
int rootValue=num[index];
TreeNode* root=new TreeNode(rootValue);
root->left=Convert(num,start,index-1);
root->right=Convert(num,index+1,end);
return root;
}
else
return NULL;
}
};
Convert Sorted Array to Binary Search Tree
最新推荐文章于 2022-02-19 22:56:38 发布