simple C++ solution with default parameters


  • 0
    J
    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums, int l=0, int r=-1) {
            if(r==-1) r=nums.size()-1;
            if(r==-1) return NULL;
            int mid=l+r>>1;
            TreeNode *ans=new TreeNode(nums[mid]);
            if(mid>l) ans->left=sortedArrayToBST(nums,l,mid-1);
            if(mid<r) ans->right=sortedArrayToBST(nums,mid+1,r);
            return ans;
        }
    };
    

Log in to reply
 

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