very simple C solution,6 lines,6 ms,only in one function


  • 0
    W
    struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
        if(numsSize<=0) return NULL;
        struct TreeNode* root=calloc(1,sizeof(struct TreeNode));
        root->val=nums[numsSize/2];
        root->left=sortedArrayToBST(nums,numsSize/2);
        root->right=sortedArrayToBST(nums+numsSize/2+1,(int)ceil(numsSize/2.0)-1);
        return root;
    }
    

Log in to reply
 

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