Postorder Traversal Visualization & Animation
A postorder traversal first visits the left child (including its entire subtree), then visits the right child (including its entire subtree), and finally visits the node itself.
## What is it?
Postorder traversal visits every node in a binary tree in **Left → Right → Root** order. The root is visited **last**, after all descendants — making it the natural choice for operations that must process children before their parent.
## How it works
- Recursively visit the **left** subtree
- Recursively visit the **right** subtree
- **Process (visit)** the current node last
```
postorder(node):
if node is null: return
postorder(node.left)
postorder(node.right)
visit(node) ← add to result last
```
## When to use
- **Delete a tree** — children must be freed before the parent
- **Compute directory sizes** — a folder's size depends on its contents
- Evaluate postfix (Reverse Polish Notation) expressions
- Bottom-up dynamic programming on trees (e.g., diameter, height, subtree sums)
- Any problem where a node's value depends on its subtree results
## Key Points
- The last element in postorder output is always the root
- Combined with inorder output, postorder uniquely identifies a binary tree
- Recursive call stack depth = O(h)
- Reverse of preorder on a mirrored tree
Category: algorithms
Difficulty: beginner
Time Complexity: O(n)
Space Complexity: O(h)
View Postorder Traversal Visualization