Binary Tree Inorder Traversal (Iterative)

Easy Trees & BST Recursion Original on LeetCode

Binary Tree Inorder Traversal (Iterative) is a easy trees & bst problem solved with the recursion pattern. The best approach, iterative (explicit stack), runs in O(n) time and O(h) space. Below are 2 approaches in Java, from recursive (reference) up.

Problem

Return the inorder traversal of a binary tree iteratively, using your own stack in place of recursion.

Examples

Example 1

Input
root = [5, 3, 8, 1, 4]
Output
[1, 3, 4, 5, 8]

Example 2

Input
root = []
Output
[]

Constraints

  • The tree has 0 to 100 nodes.
  • Solve it without recursion.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Recursive (reference)O(n)O(h)
Iterative (explicit stack)O(n)O(h)

1Recursive (reference)

TimeO(n)
SpaceO(h)

Left, node, right with the call stack.

  1. inorder(left); add; inorder(right).
Java
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> out = new ArrayList<>();
        go(root, out);
        return out;
    }

    private void go(TreeNode n, List<Integer> out) {
        if (n == null) return;
        go(n.left, out);
        out.add(n.val);
        go(n.right, out);
    }
}

2Iterative (explicit stack)

TimeO(n)
SpaceO(h)

The stack holds the nodes whose left subtree is being explored. Push down the left spine; pop gives the next node in sorted order; then continue with its right subtree.

  1. cur = root.
  2. While cur != null or the stack is not empty: while cur != null: push, cur = cur.left.
  3. cur = pop(); visit; cur = cur.right.
Java
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> out = new ArrayList<>();
        Deque<TreeNode> st = new ArrayDeque<>();
        TreeNode cur = root;
        while (cur != null || !st.isEmpty()) {
            while (cur != null) { st.push(cur); cur = cur.left; }
            cur = st.pop();
            out.add(cur.val);
            cur = cur.right;
        }
        return out;
    }
}

Edge cases to test

  • Only right children
  • Only left children

Hints

Hint 1

Keep going left, pushing nodes. When you cannot go further, pop, visit, then turn right.

FAQ

What is the best time complexity for Binary Tree Inorder Traversal (Iterative)?

Iterative (explicit stack) runs in O(n) time and O(h) extra space.

Which pattern does Binary Tree Inorder Traversal (Iterative) use?

It is a trees & bst problem that uses the recursion pattern. Other problems with the same pattern: Binary Tree Inorder Traversal (Recursive), Binary Tree Preorder Traversal (Recursive), Binary Tree Postorder Traversal (Recursive).

Is there a brute force solution for Binary Tree Inorder Traversal (Iterative)?

Yes. Recursive (reference) takes O(n) time and O(h) space. Left, node, right with the call stack.

Which edge cases should I test for Binary Tree Inorder Traversal (Iterative)?

Only right children; Only left children.