C recursive solution by numsSize


  • 0
    B
    struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
        if(numsSize==0)
            return NULL;
        struct TreeNode *t=(struct TreeNode *)malloc(sizeof(struct TreeNode));
        if(numsSize==1){
            t->val=nums[0];
            t->left=NULL;
            t->right=NULL;
            return t;
        }
        t->val=nums[numsSize/2];
        t->left=sortedArrayToBST(nums,numsSize/2);
        t->right=sortedArrayToBST(nums+numsSize/2+1,numsSize-numsSize/2-1);
        return t;
    }

Log in to reply
 

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