TimeOut && Accepted happens


  • 0
    H

    Hi everyone, first time post here.

    I used quite common approach here. Check currentNode left, change left to null push selfNode to stack , check right then change right to null and push selfNode to stack, check self then pop stack. The thing is I sometimes get timeout sometimes accepted. So Im pretty sure my approach work for all the cases, but probably not so efficient.Post my code here.

    public class Solution {
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        
        
        ArrayList<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode curNode = root;        //current node
        
        if(root == null)
            return list;
        
        while(curNode != null){
            
            if(curNode.left != null){
                
                TreeNode temp = curNode;
                stack.push(temp);
                curNode = curNode.left;
                temp.left = null;
                continue;
            }
    
            
            if(curNode.right != null){
                
                TreeNode temp = curNode;
                stack.push(temp);
                curNode = curNode.right;
                temp.right = null;
                continue;
            }
            
            list.add(curNode.val);
            
            if(stack.empty() != true)
                curNode = stack.pop();
            else
                curNode = null;
            
            
            
        }
        
        return list;
        
    }
    

    }


  • 0

    When you get Timeout, it means that the judger server is busy and is unable to process your submission, so you just have to try again at a later time.


  • 0
    H

    thank you, solved.


  • 0
    M

    The "inefficient" algorithm response is "Time Limit Exceeded," not "Timeout," in case you wanted to know.


Log in to reply
 

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