Back to All Algorithms
Post-order Traversal
Category: Tree / Graph | Time: O(n) | Space: O(h)
Visualization
Visual representation of the data structure
2
7
5
12
18
15
10
About Post-order Traversal

A tree traversal method that visits nodes in the order: left child, right child, root. It is used to delete the tree.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
Variable State
{
  "status": "starting traversal"
}
Post-order Traversal: How It Works
Last updated October 28, 2026

Overview

Post-order traversal visits the left subtree, then the right subtree, and only then the node itself. Every node is processed after all of its descendants, so information flows from the leaves up to the root.

That bottom-up order is what you need when a parent depends on results from its children: computing the height or size of a tree, evaluating an arithmetic expression tree, or freeing memory (you must delete the children before the parent that points to them).

On the default tree, post-order visits 2, 7, 5, 12, 18, 15, 10. The root, 10, comes last, and each subtree root (5 and 15) appears right after its own children.

Step by step

  1. If the current node is null, return.
  2. Recursively traverse the left subtree.
  3. Recursively traverse the right subtree.
  4. Visit the current node.

Complexity

CaseCostWhy
TimeO(n)Each node is visited once.
Space (balanced tree)O(log n)Recursion depth equals tree height.
Space (skewed tree)O(n)Height can be n.

When to use it

  • Deleting or freeing a tree safely.
  • Evaluating expression trees (it produces postfix / Reverse Polish notation).
  • Any 'combine the children's answers' computation: height, subtree sums, diameter, checking balance.

Common mistakes

  • The iterative version is the trickiest of the three depth-first traversals; a common trick is to do a modified pre-order (node, right, left) and reverse the result.
  • Mixing up post-order with reverse pre-order: they are not the same sequence.
  • Deep recursion can overflow the call stack on skewed trees.
Worked Example: Post-order Traversal Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Post-order Traversal: follow each row, predict the next one, and compare. The run below takes 60 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
Binary tree (see the input box above)
Result
final_path = [2, 7, 5, 12, 18, 15, 10]
StepLineWhat happens
11status = starting traversal
34from_node = 10, action = traversing left
54from_node = 5, action = traversing left
74from_node = 2, action = traversing left
93node = is null, action = return
105from_node = 2, action = traversing right
123node = is null, action = return
138current_node = 2, action = visiting node
1511finished_subtree_at = 2, action = return
165from_node = 5, action = traversing right
184from_node = 7, action = traversing left
203node = is null, action = return
215from_node = 7, action = traversing right
233node = is null, action = return
248current_node = 7, action = visiting node
2611finished_subtree_at = 7, action = return
278current_node = 5, action = visiting node
2911finished_subtree_at = 5, action = return
305from_node = 10, action = traversing right
324from_node = 15, action = traversing left
344from_node = 12, action = traversing left
363node = is null, action = return
375from_node = 12, action = traversing right
393node = is null, action = return
408current_node = 12, action = visiting node
4211finished_subtree_at = 12, action = return
435from_node = 15, action = traversing right
454from_node = 18, action = traversing left
473node = is null, action = return
485from_node = 18, action = traversing right
503node = is null, action = return
518current_node = 18, action = visiting node
5311finished_subtree_at = 18, action = return
548current_node = 15, action = visiting node
5611finished_subtree_at = 15, action = return
578current_node = 10, action = visiting node
5911finished_subtree_at = 10, action = return
6014final_path = [2, 7, 5, 12, 18, 15, 10]
Frequently Asked Questions
About Post-order Traversal

What is Post-order Traversal?

Post-order traversal is a depth-first traversal method for trees. The order of visiting nodes is: Left subtree, Right subtree, Root. Its most common use case is for deleting nodes from a tree.

How does Post-order traversal work?

The algorithm recursively traverses the left subtree, then recursively traverses the right subtree, and only after both subtrees are fully explored does it visit the current node.

Why is Post-order traversal used for deleting a tree?

To delete a tree, you must delete its children before you can delete the node itself. Post-order traversal ensures that a node is processed only after its children have been processed, making it perfect for this task.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.