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

  • 1

    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];
    		int top=0;
    		sta[top++] = root;
    			TreeNode now = sta[top - 1];
    			if(now!=null &&now.left != null){
    				sta[top++] = now.left;
    				now.left = null;
    			if(now != null){
    				res.add(now.val); // root may be null?
    			if(now!=null &&now.right != null){
    				sta[top++] = now.right;
    				now.right = null;
    		return res;


    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.