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 Tree

Post-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

  1. If the current node is null, return (base case).
  2. Recursively traverse the left subtree.
  3. Recursively traverse the right subtree.
  4. Visit (process) the current node last.

Post-order Worked Example

Input: Tree: root(2), left(1), right(3)

  1. Go left to node 1: visit 1
  2. Go right to node 3: visit 3
  3. Visit root last: 2

Output: 1, 3, 2

Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children — usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Trees in Code
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 traverse
Input: Tree: root(2), left(1), right(3)

Behind 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

TimeO(n)
SpaceO(h) for the recursion stack

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.