What is the reverse postorder?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Reverse postorder is the reversed sequence of nodes as they finish in a depth-first search (DFS). In postorder, a node is recorded after all its descendants are visited. Reversing this order puts parents before children, which is equivalent to topological order for directed acyclic graphs (DAGs). Reverse postorder is widely used in compiler optimizations (data flow analysis, SSA construction), topological sorting, and dependency resolution.
Postorder vs Reverse Postorder
In postorder, a node is added to the list when its DFS subtree is fully explored. Reversing this list gives an ordering where every node appears before the nodes it depends on.
Implementation in Python
The key line is postorder.append(node) — it is called after all neighbors are recursively visited, ensuring descendants are added before their parents in the postorder list.
Iterative Implementation
The iterative version uses a stack with a (node, processed) tuple. When processed is True, the node is added to the postorder list (equivalent to the recursive DFS returning).
Binary Tree Example
For a binary tree, postorder visits left-right-root. Reverse postorder visits root before children, similar to a modified preorder.
Topological Sort Connection
For DAGs, reverse postorder produces a valid topological sort — every node appears before the nodes that depend on it. This is why Kahn's algorithm and DFS-based topological sort both work.
Use in Compiler Optimization
Compilers process basic blocks in reverse postorder to ensure data flow equations converge in fewer iterations. For reducible control flow graphs, a single pass in reverse postorder computes correct results for forward data flow problems.
Reverse Postorder vs Preorder vs BFS
| Traversal | When Node is Recorded | Topological Order? |
| Preorder | When first visited | No |
| Postorder | When all descendants done | No (reversed is yes) |
| Reverse Postorder | Reversed postorder | Yes (for DAGs) |
| BFS | When dequeued | Only with Kahn's algorithm |
Common Pitfalls
- Confusing reverse postorder with reversed preorder: They are different. Preorder records nodes when first visited; postorder records when DFS backtracks. Reversing preorder does not give a valid topological sort. Only reversing postorder does.
- Applying reverse postorder to cyclic graphs for topological sort: Reverse postorder only gives a valid topological order for DAGs. For graphs with cycles, there is no valid topological order. The DFS will still complete, but the result may visit nodes in a back-edge before their dependents.
- Forgetting to handle disconnected graphs: If the graph has multiple connected components, starting DFS from a single node misses the other components. Iterate over all nodes and start DFS from any unvisited node.
- Infinite recursion on large graphs: Python's default recursion limit is 1000. For graphs with more than ~1000 nodes, use the iterative implementation or increase the limit with
sys.setrecursionlimit(). - Confusing "reverse postorder" with "reverse DFS": "Reverse DFS" sometimes means DFS on the transpose (reversed edges) graph. "Reverse postorder" means reversing the list of nodes produced by postorder DFS. These are different concepts used in different algorithms (e.g., Kosaraju's algorithm uses both).
Summary
- Reverse postorder is the reversed list of nodes as they finish in DFS
- For DAGs, reverse postorder equals topological order — every node comes before its dependents
- Implementation: run DFS, append nodes on backtrack, then reverse the list
- Compilers use reverse postorder to process basic blocks efficiently in data flow analysis
- Reverse postorder is different from reversed preorder — only the former gives topological order
- Use iterative DFS for large graphs to avoid Python's recursion limit
Related reading
- What is the right approach when using STL container for median calculation?
- What is the runtime complexity of a switch statement?
- What is the Search/Prediction Time Complexity of Logistic Regression?
- What is the significance of the semi clustering formula in the Google Pregel paper?
- What is the right way to treat Python argparse.Namespace as a dictionary?
- What is the safe way to fill multidimensional array using stdfill?
- What is the space complexity of a recursive fibonacci algorithm?
- What is the space complexity of this code?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.