My simple java without recursive solution


  • 1
    J

    import java.util.*;

    public class Solution {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode next = root;
        while (true) {
            while (next != null) {
                stack.push(next);
                next = next.left;
            }
            if (stack.size()== 0) return res;
            next = stack.pop();
            res.add(next.val);
            next = next.right;
        }
    }
    

    }


Log in to reply
 

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