[recommend for beginners]clean C++ implementation

  • 1
    class Solution {
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return help(nums, 0, nums.size()-1);
        TreeNode* help(vector<int> &nums, int start, int end){
            int _size=end-start;
            if(_size<0)    return NULL;
            if(_size==0)    return new TreeNode(nums[start]);
            int mid=(start+end)/2;
            TreeNode* root=new TreeNode(nums[mid]);
            root->left=help(nums, start, mid-1);
            root->right=help(nums, mid+1, end);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.