#145

Binary Tree Postorder Traversal

Easy Reverse of modified preorder

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the values of a binary tree in postorder (left subtree, right subtree, node).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: left, right, then node.

Why this works: node-right-left reversed equals left-right-node. Do a preorder with the children swapped and add to the front.

Example: tree 1 (left 2, right 3)

node-right-left: 1, 3, 2reversed: [2, 3, 1]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Trying the one-stack "true" postorder without tracking the last visited node — easy to loop forever.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h) for the stack plus the output.
Topic: Binary Trees
🎬 Episodes 53–60: video coming soon