Pop, add and move right


  • 0
    V
        public List<Integer> inorderTraversal(TreeNode root) 
        {
            List<Integer> toRet = new ArrayList<Integer>();
            TreeNode start = new TreeNode(0);
            start.right = root;
            Stack<TreeNode> s = new Stack<TreeNode>();
            s.push(start);
            while(s.size() > 0)
            {
                TreeNode curr = s.pop();
                toRet.add(curr.val);
                curr = curr.right;
                while(curr != null)
                {
                    s.push(curr);
                    curr = curr.left;
                }
            }
            toRet.remove(0);
            return toRet;
        }

Log in to reply
 

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