Time Limit Exceeded


  • 1
    N

    Not sure why my following solution get time limit exceeded error with Last executed input: [-97,-82,-69,-66,-37,-25,-20,-13,-12,-11,-7,-3,19,33,43,55,77]

     public TreeNode sortedArrayToBST(int[] num) {
        if(num==null || num.length==0) return null;
        return sortedArrayToBST(num, 0, num.length-1);
        
    }
     public TreeNode sortedArrayToBST(int[] num, int start, int end) {
        if(start>end) return null;
        int midIdx = start+(end-start)/2;
        TreeNode root=new TreeNode(num[midIdx]);
        root.left=sortedArrayToBST(num, 0, midIdx-1);
        root.right=sortedArrayToBST(num, midIdx+1, end);
        return root;
    }
    

    Any suggestions?


  • 3
    S

    Don't you think this statement root.left=sortedArrayToBST(num, 0, midIdx-1); should be like root.left=sortedArrayToBST(num, start, midIdx-1); ?


Log in to reply
 

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