Data Structures · Visualization
Binary Tree: Post-order
Binary Tree
Post-order traversal visits the left subtree, then the right subtree, then the current node last (Left, Right, Node). Time: O(n). Space: O(h) for the recursion stack.
Big-O: Traversals visit every node exactly once: O(n).
Binary Tree: Post-order
Binary TreePost-order traversal visits the left subtree, then the right subtree, then the current node last (Left, Right, Node). Because children are always processed before their parent, this order is ideal for tasks like safely deleting a tree or computing values that depend on child results first.
How Post-order Works
- If the current node is null, return (base case).
- Recursively traverse the left subtree.
- Recursively traverse the right subtree.
- Visit (process) the current node last.
Post-order Worked Example
Input: Tree: root(2), left(1), right(3)
- Go left to node 1: visit 1
- Go right to node 3: visit 3
- Visit root last: 2
Output: 1, 3, 2
Press ▶ Run to watch it step by step.
def postorder(n):
if not n:
return
postorder(n.left) # left
postorder(n.right) # right
print(n.val, end=" ") # root LAST
# left -> right -> root
# example: post-order traverseBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Post-order Time & Space Complexity
When to Use Post-order
- Safely deleting a tree (children freed before their parent)
- Evaluating a postfix expression tree
- Computing aggregate values, like folder sizes, from the bottom up
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.