Pre + Post + Inorder in One Traversal
Pre + Post + Inorder in One Traversal is a GFG problem (Medium). Given the root of a binary tree, return its inorder, preorder and postorder traversals, in that order, as three lists.
- All three must come from one traversal of the tree, not three separate ones.
- The tree can have up to 10⁵ nodes, so a chain-shaped tree can be 10⁵ levels deep.
- 1 <= number of nodes <= 10⁵
- 0 <= node value <= 10⁵
Intuition
A depth-first walk passes through every node three times, and each pass lines up with one traversal order:
- Arriving, before the left subtree: preorder.
- Back from the left subtree, before the right: inorder.
- Back from the right subtree, leaving: postorder.
So one walk can fill all three lists; it only has to know which of the three visits it is making. With a stack, store that as a state (1, 2 or 3) next to the node. A node pushed back with the next state sits under its child's whole subtree, so it comes up again exactly when that subtree is finished.
This problem is the clearest example of the (node, state) stack. Use the same stack whenever a problem needs work both on the way down and on the way up a tree without recursion, for example iterative postorder or computing a value after both children. The (node, state) stack is the call stack written out by hand.
Approach
Before reading on: write the recursive version that appends to all three lists. Then ask what a stack entry must remember so the iterative loop knows which of the three appends to make.
Two ways to solve it
Push (node, state) pairs and record the node in the list that matches its state.
- Depth: no recursion, so deep trees are safe.
- Shows: exactly which visit happens when.
- Cost: three pushes and pops per node.
The version the problem is really asking for.
One recursive function appends the node before, between and after its two recursive calls.
- Length: the shortest code.
- Depth: one call per level, which fails on a 10⁵-deep chain in Python.
- Shows: the three visits at a glance.
Good for explaining the idea.
Both do the same three visits per node; the stack version cannot run out of call-stack depth on a long chain. The steps, code and live demo follow the stack version; the recursive code comes after the demo.
Start with the root at state 1
Make three empty lists and a stack holding (root, 1). State 1 means the node's first visit is next.
State 1: preorder, then go left
Pop (node, 1) and append the node to pre. Push (node, 2), then push (node.left, 1) if the left child exists. The child goes on last so it comes off first, and the node waits underneath until its whole left subtree is done.
State 2: inorder, then go right
Pop (node, 2): the left subtree is finished. Append the node to ino, push (node, 3), then push (node.right, 1) if the right child exists.
State 3: postorder, and the node is done
Pop (node, 3): both subtrees are finished. Append the node to post and do not push it again. When the stack is empty, return [ino, pre, post].
Code in Python, C++ and Java
Recursive version
One function visits each node and appends it three times: to pre before the left call, to ino between the calls, and to post after the right call.
Common pitfalls
Forgetting to push the node back
if state == 1:
pre.append(node.data)
if node.left:
stack.append((node.left, 1))if state == 1:
pre.append(node.data)
stack.append((node, 2))
if node.left:
stack.append((node.left, 1))The node still needs its inorder and postorder visits. Without the push back it never comes up again, so ino and post miss it.
Pushing the child before the node
if node.left:
stack.append((node.left, 1))
stack.append((node, 2))stack.append((node, 2))
if node.left:
stack.append((node.left, 1))A stack returns the last item pushed first. With the node on top, its state-2 visit happens before its left subtree, and inorder comes out wrong.
Returning the lists in the wrong order
return [pre, ino, post]
return [ino, pre, post]
This problem asks for inorder first, then preorder, then postorder. The traversals are right but the answer is marked wrong.
Complexity
Pre + Post + Inorder in One Traversal FAQ
Why use a single stack instead of recursion?
With up to 10⁵ nodes, a chain-shaped tree makes the recursion 10⁵ calls deep, which overflows Python's default limit and can overflow the C++ or Java call stack. The (node, state) stack keeps the same three visits on the heap instead.
Is one traversal faster than three separate ones?
Both are O(n). One traversal walks the tree once instead of three times, so it does less pointer chasing, but the main point is the idea: preorder, inorder and postorder are the same walk, recorded at different moments.