/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
TreeNode* buildTree(vector<int> &vec,int start,int end){
if(start>end){
return NULL;
}
int mid=(start+end)/2;
int val=vec[mid];
TreeNode *p= new TreeNode(val);
p->left=buildTree(vec,start,mid-1);
p->right=buildTree(vec,mid+1,end);
return p;
}
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return buildTree(num,0,num.size()-1);
}
};
Convert Sorted Array to Binary Search Tree
最新推荐文章于 2022-02-19 22:56:38 发布