Algorithmspost order traversal
Postorder Traversal — Left, Right, Root
TT
Testlaa Team
May 14, 2026•1 min read
Postorder finishes both subtrees before acting at the root—natural for subtree sums, deletion, and bottom-up DP on trees.
Try this in Python
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class TreeNode:
val: int
left: TreeNode | None = None
right: TreeNode | None = None
def postorder(root: TreeNode | None) -> list[int]:
if not root:
return []
return postorder(root.left) + postorder(root.right) + [root.val]
r = TreeNode(1, TreeNode(2), TreeNode(3))
print(postorder(r))
Key takeaways
- Two-stack trick can reverse a preorder to postorder iteratively—good to sketch once.
- Many divide-and-conquer proofs on trees are post-order shaped.
- Deletion often frees children first conceptually.
Tags:
Tree structuresPythonStudents
