Introduction
LeetCode 145 – Binary Tree Postorder Traversal is one of the most important tree traversal problems for beginners learning Data Structures and Algorithms.
This problem teaches:
- Binary Tree Traversal
- Depth First Search (DFS)
- Recursion
- Stack-based traversal
- Tree traversal patterns
Postorder traversal is extremely useful in advanced tree problems such as:
- Tree deletion
- Expression tree evaluation
- Bottom-up computations
- Dynamic programming on trees
Problem Link
🔗 https://leetcode.com/problems/binary-tree-postorder-traversal/
Problem Statement
Given the root of a binary tree, return the postorder traversal of its nodes' values.
What is Postorder Traversal?
In postorder traversal, nodes are visited in this order:
Unlike preorder or inorder traversal, the root node is processed at the end.
Example
Input
Tree Structure:
Postorder Traversal
Traversal order:
Output:
Recursive Approach (Most Common)
Intuition
In postorder traversal:
- Traverse left subtree
- Traverse right subtree
- Visit current node
This naturally fits recursion because trees themselves are recursive structures.
Recursive DFS Visualization
Traversal pattern:
Recursive function:
Java Recursive Solution
Dry Run – Recursive Approach
Tree:
Step 1
Start at:
Move left:
Return back.
Step 2
Move right to:
Move left to:
Left and right of 3 are null.
Add:
Step 3
Return to:
Add:
Step 4
Return to:
Add:
Final Answer
Time Complexity – Recursive
Time Complexity
Every node is visited once.
Space Complexity
Where:
H= height of the tree- Recursive call stack uses extra space
Worst case:
for skewed trees.
Iterative Approach (Interview Follow-Up)
The follow-up asks:
Can you solve it iteratively?
Yes.
We use stacks to simulate recursion.
Iterative Postorder Intuition
Postorder traversal order is:
One common trick is:
- Traverse in modified preorder:
- Reverse the result.
After reversing, we get:
which is postorder traversal.
Stack-Based Iterative Logic
Algorithm
- Push root into stack.
- Pop node.
- Add node value to answer.
- Push left child.
- Push right child.
- Reverse final answer.
Java Iterative Solution
Dry Run – Iterative Approach
Tree:
Step 1
Push:
Step 2
Pop:
Add at front:
Push right child:
Step 3
Pop:
Add at front:
Push left child:
Step 4
Pop:
Add at front:
Final Answer
Comparison of Approaches
| Approach | Advantages | Disadvantages |
| Recursive | Easy to understand | Uses recursion stack |
| Iterative | Better interview practice | Slightly harder logic |
Interview Explanation
In interviews, explain:
Postorder traversal processes nodes in Left → Right → Root order. Recursion naturally handles this traversal. Iteratively, we simulate recursion using a stack and reverse traversal order.
This demonstrates strong tree traversal understanding.
Common Mistakes
1. Wrong Traversal Order
Incorrect:
That is preorder traversal.
Correct postorder:
2. Forgetting Null Base Case
Always check:
3. Incorrect Stack Push Order
For iterative solution:
- Push left first
- Push right second
because we reverse the result later.
FAQs
Q1. Why is postorder traversal useful?
It is used in:
- Tree deletion
- Expression tree evaluation
- Bottom-up dynamic programming
- Calculating subtree information
Q2. Which approach is preferred in interviews?
Recursive is simpler.
Iterative is often asked as a follow-up.
Q3. Can postorder traversal be done without stack or recursion?
Yes.
Using Morris Traversal.
Q4. What is the difference between preorder, inorder, and postorder?
| Traversal | Order |
| Preorder | Root → Left → Right |
| Inorder | Left → Root → Right |
| Postorder | Left → Right → Root |
Bonus: Morris Postorder Traversal
Morris traversal performs tree traversal using:
extra space.
This is considered an advanced interview topic.
Conclusion
LeetCode 145 is an excellent beginner-friendly tree traversal problem.
It teaches:
- DFS traversal
- Recursion
- Stack simulation
- Binary tree fundamentals
The key postorder pattern is:
Mastering this traversal helps in solving many advanced tree problems such as:
- Tree DP
- Tree deletion
- Expression evaluation
- Subtree calculations
- Advanced DFS problems





