Skip to content
Veltoryx Technologies
FreeProblem 90● EasyLeetCode #145 ↗3 min readUpdated 30 October 2026

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.

text
       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 visit Node -> Right -> Left and reverse the resulting sequence (or insert each node at the front of a linked list), the result is exactly Left -> Right -> Node!

03Pseudocode

text
stack = [root]
while stack:
  n = pop()
  add n.val to the FRONT of out
  push n.left ; push n.right
return out

04Watch it work

Visualizer · step through the algorithmtree = [1,2,3,4,5,null,6]

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

  1. stack = [root]
  2. while stack:
  3. n = pop()
  4. add n.val to the FRONT of out
  5. push n.left ; push n.right
  6. return out

Variables

root
1
step 1 / 8

05Step-by-step dry run

We trace on tree [1, 2, 3, 4, 5, null, 6]:

Popped nodeStack (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

java
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));
    }
}
Expected output
[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, and LinkedList.addFirst() is O(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 an ArrayList takes O(n) per operation, making total time O(n^2). Always use LinkedList.addFirst() or append to an ArrayList and call Collections.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.