what is wrong with my code?


  • 0
    D

    Can anybody tell what is wrong with my code? Submission says it fails at test case [-1,0,1,2]. But when I run the test case alone, my output is same with the expected one.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
        int mid = 0;
        struct TreeNode* node;
        
        if (numsSize == 0) 
            return NULL;
        node = (struct TreeNode*)malloc (sizeof (struct TreeNode));
        mid = (numsSize-1)/2;
        if (numsSize == 1)
        {
            node->val = nums[mid];
        }
        else
        {
            node->val = nums[mid];
            node->left = sortedArrayToBST(nums, mid);
            node->right = sortedArrayToBST(nums+mid+1, numsSize - 1 - mid);       
        }
        
        return node;
        
    }
    

Log in to reply
 

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