GeeksforGeeks Medium

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.
Constraints
  • 1 <= number of nodes <= 10⁵
  • 0 <= node value <= 10⁵
treestacktraversal
Open on GeeksforGeeks ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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].

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def treeTraversal(self, root):
▶3 pre, ino, post = [], [], []
▶4 stack = [(root, 1)]
▶5 while stack:
▶6 node, state = stack.pop()
▶7 if state == 1:
▶8 pre.append(node.data)
▶9 stack.append((node, 2))
▶10 if node.left:
▶11 stack.append((node.left, 1))
▶12 elif state == 2:
▶13 ino.append(node.data)
▶14 stack.append((node, 3))
▶15 if node.right:
▶16 stack.append((node.right, 1))
▶17 else:
▶18 post.append(node.data)
▶19 return [ino, pre, post]
1234567stack1state 1preinpoststack = [(1, state 1)]
stack(1, 1)root, first visit next
nodes7each visited 3 times
Each stack entry is a node plus its state: which of its three visits comes next. State 1 is the visit before its left subtree, state 2 between the subtrees, state 3 after both.
1234567stack1state 22state 1pre1inpostpop (1, 1) → preorder gets 1
popped(1, 1)before left subtree
preorder[1]
stack2entries
Pop (1, 1): the first visit, so 1 goes into preorder. Push (1, 2) back first, then its left child (2, 1) on top, so the whole left subtree is handled before 1 comes up again.
1234567stack1state 22state 24state 1pre12inpostpop (2, 1) → preorder gets 2
popped(2, 1)before left subtree
preorder[1, 2]
stack3entries
Pop (2, 1): the first visit, so 2 goes into preorder. Push (2, 2) back first, then its left child (4, 1) on top, so the whole left subtree is handled before 2 comes up again.
1234567stack1state 22state 24state 2pre124inpostpop (4, 1) → preorder gets 4
popped(4, 1)before left subtree
preorder[1, 2, 4]
stack3entries
Pop (4, 1): the first visit, so 4 goes into preorder. Push (4, 2) back first, then nothing else: it has no left child, so 4 comes straight back up.
1234567stack1state 22state 24state 3pre124in4postpop (4, 2) → inorder gets 4
popped(4, 2)between subtrees
inorder[4]
stack3entries
Pop (4, 2): the left subtree is finished, so 4 goes into inorder. Push (4, 3) back, then nothing else: it has no right child.
1234567stack1state 22state 2pre124in4post4pop (4, 3) → postorder gets 4
popped(4, 3)after both subtrees
postorder[4]
stack2entries
Pop (4, 3): both subtrees are finished, so 4 goes into postorder. It is not pushed again; 4 is done.
1234567stack1state 22state 35state 1pre124in42post4pop (2, 2) → inorder gets 2
popped(2, 2)between subtrees
inorder[4, 2]
stack3entries
Pop (2, 2): the left subtree is finished, so 2 goes into inorder. Push (2, 3) back, then its right child (5, 1) on top.
1234567stack1state 22state 35state 2pre1245in42post4pop (5, 1) → preorder gets 5
popped(5, 1)before left subtree
preorder[1, 2, 4, 5]
stack3entries
Pop (5, 1): the first visit, so 5 goes into preorder. Push (5, 2) back first, then nothing else: it has no left child, so 5 comes straight back up.
1234567stack1state 22state 35state 3pre1245in425post4pop (5, 2) → inorder gets 5
popped(5, 2)between subtrees
inorder[4, 2, 5]
stack3entries
Pop (5, 2): the left subtree is finished, so 5 goes into inorder. Push (5, 3) back, then nothing else: it has no right child.
1234567stack1state 22state 3pre1245in425post45pop (5, 3) → postorder gets 5
popped(5, 3)after both subtrees
postorder[4, 5]
stack2entries
Pop (5, 3): both subtrees are finished, so 5 goes into postorder. It is not pushed again; 5 is done.
1234567stack1state 2pre1245in425post452pop (2, 3) → postorder gets 2
popped(2, 3)after both subtrees
postorder[4, 5, 2]
stack1entries
Pop (2, 3): both subtrees are finished, so 2 goes into postorder. It is not pushed again; 2 is done.
1234567stack1state 33state 1pre1245in4251post452pop (1, 2) → inorder gets 1
popped(1, 2)between subtrees
inorder[4, 2, 5, 1]
stack2entries
Pop (1, 2): the left subtree is finished, so 1 goes into inorder. Push (1, 3) back, then its right child (3, 1) on top.
1234567stack1state 33state 26state 1pre12453in4251post452pop (3, 1) → preorder gets 3
popped(3, 1)before left subtree
preorder[1, 2, 4, 5, 3]
stack3entries
Pop (3, 1): the first visit, so 3 goes into preorder. Push (3, 2) back first, then its left child (6, 1) on top, so the whole left subtree is handled before 3 comes up again.
1234567stack1state 33state 26state 2pre124536in4251post452pop (6, 1) → preorder gets 6
popped(6, 1)before left subtree
preorder[1, 2, 4, 5, 3, 6]
stack3entries
Pop (6, 1): the first visit, so 6 goes into preorder. Push (6, 2) back first, then nothing else: it has no left child, so 6 comes straight back up.
1234567stack1state 33state 26state 3pre124536in42516post452pop (6, 2) → inorder gets 6
popped(6, 2)between subtrees
inorder[4, 2, 5, 1, 6]
stack3entries
Pop (6, 2): the left subtree is finished, so 6 goes into inorder. Push (6, 3) back, then nothing else: it has no right child.
1234567stack1state 33state 2pre124536in42516post4526pop (6, 3) → postorder gets 6
popped(6, 3)after both subtrees
postorder[4, 5, 2, 6]
stack2entries
Pop (6, 3): both subtrees are finished, so 6 goes into postorder. It is not pushed again; 6 is done.
1234567stack1state 33state 37state 1pre124536in425163post4526pop (3, 2) → inorder gets 3
popped(3, 2)between subtrees
inorder[4, 2, 5, 1, 6, 3]
stack3entries
Pop (3, 2): the left subtree is finished, so 3 goes into inorder. Push (3, 3) back, then its right child (7, 1) on top.
1234567stack1state 33state 37state 2pre1245367in425163post4526pop (7, 1) → preorder gets 7
popped(7, 1)before left subtree
preorder[1, 2, 4, 5, 3, 6, 7]
stack3entries
Pop (7, 1): the first visit, so 7 goes into preorder. Push (7, 2) back first, then nothing else: it has no left child, so 7 comes straight back up.
1234567stack1state 33state 37state 3pre1245367in4251637post4526pop (7, 2) → inorder gets 7
popped(7, 2)between subtrees
inorder[4, 2, 5, 1, 6, 3, 7]
stack3entries
Pop (7, 2): the left subtree is finished, so 7 goes into inorder. Push (7, 3) back, then nothing else: it has no right child.
1234567stack1state 33state 3pre1245367in4251637post45267pop (7, 3) → postorder gets 7
popped(7, 3)after both subtrees
postorder[4, 5, 2, 6, 7]
stack2entries
Pop (7, 3): both subtrees are finished, so 7 goes into postorder. It is not pushed again; 7 is done.
1234567stack1state 3pre1245367in4251637post452673pop (3, 3) → postorder gets 3
popped(3, 3)after both subtrees
postorder[4, 5, 2, 6, 7, 3]
stack1entries
Pop (3, 3): both subtrees are finished, so 3 goes into postorder. It is not pushed again; 3 is done.
1234567stackemptypre1245367in4251637post4526731pop (1, 3) → postorder gets 1
popped(1, 3)after both subtrees
postorder[4, 5, 2, 6, 7, 3, 1]
stack0entries
Pop (1, 3): both subtrees are finished, so 1 goes into postorder. It is not pushed again; 1 is done.
1234567stackemptypre1245367in4251637post4526731stack empty → return [in, pre, post]
inorder[4, 2, 5, 1, 6, 3, 7]
preorder[1, 2, 4, 5, 3, 6, 7]
postorder[4, 5, 2, 6, 7, 3, 1]
Stack empty. Every node was popped exactly three times, once per list, so all three orders came out of one pass in O(n) time.
05

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.

▶1class Solution:
▶2 def treeTraversal(self, root):
▶3 pre, ino, post = [], [], []
▶4 
▶5 def walk(node):
▶6 if not node:
▶7 return
▶8 pre.append(node.data)
▶9 walk(node.left)
▶10 ino.append(node.data)
▶11 walk(node.right)
▶12 post.append(node.data)
▶13 
▶14 walk(root)
▶15 return [ino, pre, post]
06

Common pitfalls

Forgetting to push the node back

✗ Wrong
if state == 1:
    pre.append(node.data)
    if node.left:
        stack.append((node.left, 1))
✓ Right
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

✗ Wrong
if node.left:
    stack.append((node.left, 1))
stack.append((node, 2))
✓ Right
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

✗ Wrong
return [pre, ino, post]
✓ Right
return [ino, pre, post]

This problem asks for inorder first, then preorder, then postorder. The traversals are right but the answer is marked wrong.

07

Complexity

Time
O(n)
Space
O(h)
Each node is pushed and popped three times. The stack holds about one entry per node on the current path, so it grows with the height h (up to n for a chain). The three output lists take O(n) on top.
08

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.