LeetCode 145: Binary Tree Postorder Traversal – Java Recursive & Iterative Solution Explained

Learn how to solve LeetCode 145 Binary Tree Postorder Traversal using Recursive and Iterative approaches in Java. Includes traversal intuition, stack-based solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
2 views
LinkedInGithubX
0
0
LeetCode 145: Binary Tree Postorder Traversal – Java Recursive & Iterative Solution Explained
Listen to articleAudio version
Ad
Advertisement

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:

  1. Binary Tree Traversal
  2. Depth First Search (DFS)
  3. Recursion
  4. Stack-based traversal
  5. Tree traversal patterns

Postorder traversal is extremely useful in advanced tree problems such as:

  1. Tree deletion
  2. Expression tree evaluation
  3. Bottom-up computations
  4. 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:

Left → Right → Root

Unlike preorder or inorder traversal, the root node is processed at the end.

Example

Input

root = [1,null,2,3]

Tree Structure:

1
\
2
/
3

Postorder Traversal

Traversal order:

3 → 2 → 1

Output:

[3,2,1]

Recursive Approach (Most Common)

Intuition

In postorder traversal:

  1. Traverse left subtree
  2. Traverse right subtree
  3. Visit current node

This naturally fits recursion because trees themselves are recursive structures.

Recursive DFS Visualization

Traversal pattern:

Left → Right → Root

Recursive function:

postorder(node.left)
postorder(node.right)
visit(node)

Java Recursive Solution

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* }
*/

class Solution {

public void solve(List<Integer> list, TreeNode root) {

if(root == null) return;

solve(list, root.left);

solve(list, root.right);

list.add(root.val);
}

public List<Integer> postorderTraversal(TreeNode root) {

List<Integer> list = new ArrayList<>();

solve(list, root);

return list;
}
}

Dry Run – Recursive Approach

Tree:

1
\
2
/
3

Step 1

Start at:

1

Move left:

null

Return back.

Step 2

Move right to:

2

Move left to:

3

Left and right of 3 are null.

Add:

3

Step 3

Return to:

2

Add:

2

Step 4

Return to:

1

Add:

1

Final Answer

[3,2,1]

Time Complexity – Recursive

Time Complexity

O(N)

Every node is visited once.

Space Complexity

O(H)

Where:

  1. H = height of the tree
  2. Recursive call stack uses extra space

Worst case:

O(N)

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:

Left → Right → Root

One common trick is:

  1. Traverse in modified preorder:
Root → Right → Left
  1. Reverse the result.

After reversing, we get:

Left → Right → Root

which is postorder traversal.

Stack-Based Iterative Logic

Algorithm

  1. Push root into stack.
  2. Pop node.
  3. Add node value to answer.
  4. Push left child.
  5. Push right child.
  6. Reverse final answer.

Java Iterative Solution

class Solution {

public List<Integer> postorderTraversal(TreeNode root) {

LinkedList<Integer> ans = new LinkedList<>();

if(root == null) return ans;

Stack<TreeNode> stack = new Stack<>();

stack.push(root);

while(!stack.isEmpty()) {

TreeNode node = stack.pop();

ans.addFirst(node.val);

if(node.left != null) {
stack.push(node.left);
}

if(node.right != null) {
stack.push(node.right);
}
}

return ans;
}
}

Dry Run – Iterative Approach

Tree:

1
\
2
/
3

Step 1

Push:

1

Step 2

Pop:

1

Add at front:

[1]

Push right child:

2

Step 3

Pop:

2

Add at front:

[2,1]

Push left child:

3

Step 4

Pop:

3

Add at front:

[3,2,1]

Final Answer

[3,2,1]

Comparison of Approaches

ApproachAdvantagesDisadvantages
RecursiveEasy to understandUses recursion stack
IterativeBetter interview practiceSlightly 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:

Root → Left → Right

That is preorder traversal.

Correct postorder:

Left → Right → Root

2. Forgetting Null Base Case

Always check:

if(root == null) return;

3. Incorrect Stack Push Order

For iterative solution:

  1. Push left first
  2. Push right second

because we reverse the result later.

FAQs

Q1. Why is postorder traversal useful?

It is used in:

  1. Tree deletion
  2. Expression tree evaluation
  3. Bottom-up dynamic programming
  4. 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?

TraversalOrder
PreorderRoot → Left → Right
InorderLeft → Root → Right
PostorderLeft → Right → Root

Bonus: Morris Postorder Traversal

Morris traversal performs tree traversal using:

O(1)

extra space.

This is considered an advanced interview topic.

Conclusion

LeetCode 145 is an excellent beginner-friendly tree traversal problem.

It teaches:

  1. DFS traversal
  2. Recursion
  3. Stack simulation
  4. Binary tree fundamentals

The key postorder pattern is:

Left → Right → Root

Mastering this traversal helps in solving many advanced tree problems such as:

  1. Tree DP
  2. Tree deletion
  3. Expression evaluation
  4. Subtree calculations
  5. Advanced DFS problems
Ai Assistant Kas