Data Structures19 sections · 729 units
Open in Course

Iterative Inorder

Simulating the call stack

The iterative approach uses an explicit stack to simulate recursion:

function inorderIterative(root):
    result = []
    stack = []
    current = root
    while current or stack:
        while current:
            stack.push(current)
            current = current.left
        current = stack.pop()
        result.append(current.val)
        current = current.right
    return result

The inner while loop pushes all left descendants. when you can't go left anymore, you pop, process, and go right.

Why learn this? Some problems require modifying traversal mid-process, which is easier iteratively. Also, understanding how recursion maps to iteration deepens your grasp of both.