Binary Tree Postorder Traversal
Return the postorder traversal of a binary tree iteratively using an explicit stack.
01The problem
Given the root of a binary tree, return the postorder traversal of its nodes' values: left subtree, right subtree, then the node last. Implement it iteratively.
1
/ 2 3
/ \ 4 5 6
Postorder: [4, 5, 2, 6, 3, 1]Constraints
- The number of nodes in the tree is in the range
[0, 100]. -100 <= Node.val <= 100
02How to spot it
- "Postorder traversal" / "Left -> Right -> Node": Children must be visited before their parent.
- The reverse preorder insight: Preorder visits
Node -> Left -> Right. If we modify preorder to visitNode -> Right -> Leftand reverse the resulting sequence (or insert each node at the front of a linked list), the result is exactlyLeft -> Right -> Node!
03Pseudocode
stack = [root]
while stack:
n = pop()
add n.val to the FRONT of out
push n.left ; push n.right
return out04Watch it work
popped node
none
stack (node -> right -> left order)
output (prepended to front)
status
start
Initialize stack with root 1. Postorder is reversed modified-preorder.
Pseudocode
- stack = [root]
- while stack:
- n = pop()
- add n.val to the FRONT of out
- push n.left ; push n.right
- return out
Variables
- root
- 1
05Step-by-step dry run
We trace on tree [1, 2, 3, 4, 5, null, 6]:
| Popped node | Stack (top to bottom) | out (prepended at front) | Notes |
|---|---|---|---|
| Init | [1] | [] | Start at root |
1 | [3, 2] | [1] | Push left (2) then right (3) |
3 | [6, 2] | [3, 1] | Prepend 3; push right (6) |
6 | [2] | [6, 3, 1] | Prepend 6 (leaf) |
2 | [5, 4] | [2, 6, 3, 1] | Prepend 2; push left (4), right (5) |
5 | [4] | [5, 2, 6, 3, 1] | Prepend 5 (leaf) |
4 | [] | [4, 5, 2, 6, 3, 1] | Prepend 4 (leaf) |
Final result: [4, 5, 2, 6, 3, 1].
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> postorderTraversal(TreeNode root) {
LinkedList<Integer> out = new LinkedList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode n = stack.pop();
out.addFirst(n.val);
if (n.left != null) {
stack.push(n.left);
}
if (n.right != null) {
stack.push(n.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(postorderTraversal(root));
}
}[4, 5, 2, 6, 3, 1]
07Why it works
Traversing in order Node -> Right -> Left generates the exact mathematical reverse of postorder (Left -> Right -> Node). By pushing n.left before n.right on the stack, n.right pops first. By prepending each popped value to the front using LinkedList.addFirst(), we reverse the traversal on the fly in O(1) per insertion.
08Time and space
- Time complexity:
O(n). Every node is pushed and popped once, andLinkedList.addFirst()isO(1). - Space complexity:
O(h)for the stack storing tree ancestors.
09Edge cases
- Empty tree: Returns empty list
[]. - Single node: Pops root, prepends value, returns
[root.val].
10Common mistakes
- Using
ArrayList.add(0, val): Inserting at index 0 of anArrayListtakesO(n)per operation, making total timeO(n^2). Always useLinkedList.addFirst()or append to anArrayListand callCollections.reverse(out)at the end.
11Other approaches
- One-stack with previous pointer: Tracks the previously visited node to know whether we are returning from the left or right child.
12In the interview
- Opening line: "Postorder is the reverse of a modified preorder traversal (Node -> Right -> Left). I will run modified preorder with a stack and insert at the front of a LinkedList."
13Quiz
Why does pushing left child before right child on the stack result in postorder when prepending to the output list?
- (Recommended) The stack pops right child first (Node -> Right -> Left order), which reversed is postorder (Left -> Right -> Node)
- Because left children are always smaller in value
- Because postorder does not require stack memory
- Because the root is visited twice
Answer: The stack pops right child first (Node -> Right -> Left order), which reversed is postorder (Left -> Right -> Node).
14Solve it yourself
Open LeetCode #145 and submit the iterative two-pointer or reversed preorder solution.