It works on local, but not in OJ : ERROR -- Last executed input: []


  • 1
    H

    It is weird that my code works on local, but not in OJ, it says that "Last executed input: []", which means the input is empty, but actually, it is impossible.
    [I use two functions to implement this ]

    Thanks for any advice! :-)

    public class SortedArrayToBST 
        {
        	public TreeNode sortedArrayToBST(int[] num) 
        	{
        		int start = 0;
        		int end = num.length - 1;
        		int middle = (end + start) / 2;
        		TreeNode tn = new TreeNode(num[middle]);
        		tn.left = new TreeNode(0);
        		convert(tn.left , num, start, middle - 1);
        		tn.right = new TreeNode(-1);
        		convert(tn.right, num, middle + 1, end);
        		return tn;
        
        	}
        
        	public void convert(TreeNode tn, int[] num, int start, int end)
        	{
        		if(end >= start)
        		{
        			int middle = (end + start) / 2;
        			tn.val = num[middle];
        			System.out.println("###" + tn.val);
        			if(start < middle)
        			{
        				tn.left = new TreeNode(-1);
        				convert(tn.left , num, start, middle - 1);
        			}
        			if(end > middle)
        			{
        				tn.right = new TreeNode(-1);
        				convert(tn.right, num, middle + 1, end);
        			}
        		}
        	}
        
        	public void midList(TreeNode tn)
        	{
        		
        		if(tn.left != null)
        		{
        			midList(tn.left);
        		}
        		System.out.println(tn.val);
        		if(tn.right != null)
        		{
        			midList(tn.right);
        		}
        	}
        
        	public static void main(String[] args)
        	{
        		SortedArrayToBST satb = new SortedArrayToBST();
        		int[] num = new int[20];
        		int len = num.length;
        		for(int i = 0; i < len; i++)
        		{
        			num[i] = i + 1;
        		}
        		satb.midList(satb.sortedArrayToBST(num));
        	}
        }
        
        class TreeNode 
        {
        	int val;
        	TreeNode left;
        	TreeNode right;
        	TreeNode(int x) { val = x; }
        }

  • 1
    S

    What do you mean about impossible. When the input array is empty, the length is 0. I am afraid you do not consider such edge case.


  • 0
    H

    Oh, thanks, you mean OJ will never cares my custom "main()" function? :-)


  • 0
    S

    Yes, do not care about your main. However, you may need to clean your code when submit to OJ to avoid some unexpected errors.


Log in to reply
 

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