Please help in optimizing this code


  • 0
    D
    public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> trav =  new ArrayList<Integer>();
    if(root==null){
        return trav;
    }
    
    Stack<TreeNode> inorder = new Stack<TreeNode>();
    
    inorder.push(root);
    TreeNode node = null;
    while(!inorder.empty()){
        node=inorder.pop();
    
        if(node.right!=null){
            inorder.push(node.right);
        }
        if(node.left!=null){
            inorder.push(node);
            inorder.push(node.left);
        }else{
            trav.add(node.val);
        }
    }
    
    return trav;
        
    }
    

    }


  • 0
    D

    Getting time out.


  • 0
    X

    Your Program trap in a deap loop。set the child null can solve this problem


Log in to reply
 

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