Binary Tree Postorder Traversal (Recursive)
Binary Tree Postorder Traversal (Recursive) is a easy trees & bst problem solved with the recursion pattern.
The best approach, iterative (reverse of root-right-left), runs in O(n) time and O(n) space.
Below are 2 approaches in Java, from recursive dfs up.
Problem
Return the postorder traversal (left, right, node) of a binary tree.
Examples
Example 1
- Input
root = [4, 2, 6, 1, 3]- Output
[1, 3, 2, 6, 4]
Example 2
- Input
root = [1]- Output
[1]
Constraints
- The tree has
0to100nodes.
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.
| Approach | Time | Space |
|---|---|---|
| Recursive DFS | O(n) | O(h) |
| Iterative (reverse of root-right-left) | O(n) | O(n) |
1Recursive DFS
O(n)O(h)Visit both children, then record the node.
- if node == null return.
- postorder(left); postorder(right); add(node.val).
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
postorder(root, out);
return out;
}
private void postorder(TreeNode node, List<Integer> out) {
if (node == null) return;
postorder(node.left, out);
postorder(node.right, out);
out.add(node.val);
}
}2Iterative (reverse of root-right-left)
O(n)O(n)A root, right, left preorder is exactly postorder reversed. Run that with a stack and add each value to the front of the list.
- Push root; pop, addFirst(val), push left, push right.
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> out = new LinkedList<>();
if (root == null) return out;
Deque<TreeNode> st = new ArrayDeque<>();
st.push(root);
while (!st.isEmpty()) {
TreeNode n = st.pop();
out.addFirst(n.val);
if (n.left != null) st.push(n.left);
if (n.right != null) st.push(n.right);
}
return out;
}
}Edge cases to test
- Empty tree
Hints
Hint 1
Postorder = left, right, then node. A node is handled only after both its subtrees, which is why height, diameter and balance checks use it.
FAQ
What is the best time complexity for Binary Tree Postorder Traversal (Recursive)?
Iterative (reverse of root-right-left) runs in O(n) time and O(n) extra space.
Which pattern does Binary Tree Postorder Traversal (Recursive) 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 Inorder Traversal (Iterative).
Is there a brute force solution for Binary Tree Postorder Traversal (Recursive)?
Yes. Recursive DFS takes O(n) time and O(h) space. Visit both children, then record the node.
Which edge cases should I test for Binary Tree Postorder Traversal (Recursive)?
Empty tree.