Clean Recursive C++ Solution

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

Log in to reply

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