[Java Solution] using Stack Iteration. push left nodes-> pop out-> push right nodes


  • 0
    public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        Stack<TreeNode> s=new Stack<TreeNode>();
        if(root==null)
            return res;
        pushLeftTree(root,s);
        TreeNode node;
        while(!s.empty()){
            node=s.pop();
            res.add(node.val);
            if(node.right!=null){
               pushLeftTree(node.right,s);
            }
                
        }
        return res;
    }
    private void pushLeftTree(TreeNode root, Stack s){
        TreeNode node=root;
        while(node!=null){
            s.push(node);
            node=node.left;
        }
    }        
    

    }


Log in to reply
 

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