Binary Tree Inorder Traversal
Return the inorder traversal of a binary tree iteratively using an explicit stack.
01The problem
Given the root of a binary tree, return the inorder traversal of its nodes' values: left subtree, node, then right subtree. In a Binary Search Tree (BST), this visits nodes in strictly increasing sorted order. Implement it iteratively with a stack.
1
/ 2 3
/ \ 4 5 6
Inorder: [4, 2, 5, 1, 3, 6]Constraints
- The number of nodes in the tree is in the range
[0, 100]. -100 <= Node.val <= 100
02How to spot it
- "Inorder traversal" / "Left -> Node -> Right": A node cannot be visited until its entire left subtree has been processed.
- Iterative left-spine push: Keep going left until hitting
null, pushing each ancestor to the stack. When hittingnull, pop one node, visit it, and move to its right child.
03Pseudocode
cur = root
while cur or stack:
while cur: push cur ; cur = cur.left
cur = pop()
visit cur
cur = cur.right04Watch it work
current cur
1
stack (left spine ancestors)
inorder traversal
status
start at root
Start iterative inorder traversal with cur = root (1).
Pseudocode
- cur = root
- while cur or stack:
- while cur: push cur ; cur = cur.left
- cur = pop()
- visit cur
- cur = cur.right
Variables
- cur
- 1
05Step-by-step dry run
We trace on tree [1, 2, 3, 4, 5, null, 6]:
| Action | cur pointer | Stack | Output |
|---|---|---|---|
| Descend left spine | 1 -> 2 -> 4 -> null | [1, 2, 4] | [] |
| Pop 4 | 4 | [1, 2] | [4] |
cur = 4.right (null), pop 2 | 2 | [1] | [4, 2] |
cur = 2.right (5), push 5, pop 5 | 5 | [1] | [4, 2, 5] |
cur = 5.right (null), pop 1 | 1 | [] | [4, 2, 5, 1] |
cur = 1.right (3), push 3, descend | 3 | [3] | [4, 2, 5, 1] |
Pop 3, cur = 3.right (6), push 6 | 6 | [] | [4, 2, 5, 1, 3] |
Pop 6, cur = 6.right (null) | null | [] | [4, 2, 5, 1, 3, 6] |
06Java solution
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class Solution {
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
out.add(cur.val);
cur = cur.right;
}
return out;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1,
new TreeNode(2, new TreeNode(4), new TreeNode(5)),
new TreeNode(3, null, new TreeNode(6))
);
System.out.println(inorderTraversal(root));
}
}[4, 2, 5, 1, 3, 6]
07Why it works
A node can only be processed after its left subtree is exhausted. The inner loop while (cur != null) buffers all ancestors along the left spine on the stack. When cur becomes null, the stack top is guaranteed to be the leftmost unvisited node. We pop and visit it, then set cur = cur.right to repeat the process for its right subtree.
08Time and space
- Time complexity:
O(n). Each node is pushed onto the stack once and popped once. - Space complexity:
O(h)auxiliary space for the stack storing the longest root-to-leaf path (O(log n)balanced,O(n)skewed).
09Edge cases
- Empty tree: Loop condition
cur != null || !stack.isEmpty()is false initially; returns[]. - Right-skewed tree: Each step pushes 1 node and immediately pops it, visiting in order
1, 2, 3....
10Common mistakes
- Forgetting
cur != nullin outer while loop: If you only check!stack.isEmpty(), the loop terminates prematurely when the stack is momentarily empty ascurmoves to a right subtree.
11Other approaches
- Morris Inorder Traversal: Links the rightmost node of the left subtree (inorder predecessor) to the current node, traversing with
O(1)memory.
12In the interview
- Opening line: "To do inorder iteratively, I maintain a pointer
curand a stack. I push nodes down the left spine until null, pop and visit the top, then shift to the right child." - Key point: Emphasize that inorder traversal of a BST yields elements in sorted order.
13Quiz
Why does the outer loop check cur != null || !stack.isEmpty() instead of just !stack.isEmpty()?
- (Recommended) Because when popping the root of a subtree, the stack can be empty while
curis exploring its right child - Because trees have negative values
- Because the stack stores children instead of ancestors
- Because Java deques do not support isEmpty checks
Answer: Because when popping the root of a subtree, the stack can be empty while cur is exploring its right child.
14Solve it yourself
Try LeetCode #94 and submit the iterative solution.