Binary Tree Postorder Traversal – Solution & Complexity
Solution Walkthrough
Step 1: Recursive depth-first traversal
Postorder traversal is defined recursively: traverse the left subtree, then the right subtree, then record the current node. A helper DFS mirrors that definition directly and is the easiest way to get started.
Step 2: Single-stack iterative solution
To avoid recursion, simulate the call stack yourself. Walk left as far as possible, peek at the top node, and only visit it after its right subtree is done. Tracking the last visited node lets one stack handle the full postorder order, which is more efficient than using a second stack but also a bit trickier to reason about.