I can run it normally on my computer,why is Time Limit Exceeded?


  • 1
    V

    https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
    I use the loop to solve this problem.I can run it normally on my computer.But I submit it,system response the result of Time Limit Exceeded.

    public class Solution {
        private static int MAX = 10000000;
    	public List<Integer> inorderTraversal(TreeNode root) {
    		List<Integer> res = new ArrayList<Integer>();
    		TreeNode sta[] = new TreeNode[MAX];
    		//init
    		int top=0;
    		sta[top++] = root;
    		
    		while(top>0){
    			TreeNode now = sta[top - 1];
    			if(now!=null &&now.left != null){
    				sta[top++] = now.left;
    				now.left = null;
    				continue;
    			}
    			if(now != null){
    				res.add(now.val); // root may be null?
    				//System.out.println(now.val);
    			}
    			top--;
    			if(now!=null &&now.right != null){
    				sta[top++] = now.right;
    				now.right = null;
    			}
    		}
    		return res;
    	}
    }
    

    result:

    Last executed input: {1,4,3,2}

    But,as you can see,this case doesn't make my code Time Limit Exceeded.Can anyone tell me why?


Log in to reply
 

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