Binary Tree Postorder Traversal
Binary Tree Postorder Traversal is LeetCode 145 (Easy). Given the root of a binary tree, return its node values in postorder: the left subtree, then the right subtree, then the node.
- The tree can be empty; then the answer is
[]. - Follow-up: the recursive solution is trivial, so do it iteratively.
With at most 100 nodes any O(n) walk is fast; the hard part is that a node must wait for both of its subtrees.
- The number of the nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Intuition
An iterative postorder traversal keeps waiting nodes on a stack, as inorder does. The difference is that a node on top of the stack can be in two situations:
- Right subtree not visited yet: leave the node where it is and go into its right child first.
- Right subtree finished, or missing: the node is ready; pop it and record it.
A pointer last, the node recorded most recently, tells the two apart. A subtree's root is always the last of its nodes to be recorded, so the right subtree is finished exactly when the right child is last.
Postorder is the order for anything a node computes from its children's answers: a tree's height, subtree sums, checking whether a tree is balanced, deleting a tree, evaluating an expression tree. Maximum Depth of Binary Tree and Diameter of Binary Tree are postorder walks written as recursion.
Approach
Before reading on: in [1,null,2,3] the answer is [3, 2, 1]. Node 1 is reached first but recorded last. When 1 is on top of your stack and its right child 2 still exists, how can you tell whether 2's subtree is still to do or already finished?
Two ways to solve it
Walk left pushing nodes, then record the top once its right subtree is done.
- Follow-up: the iterative answer LeetCode asks for.
- Depth: no recursion limit on a chain-shaped tree.
- Order: nodes come out in postorder as you go.
The version to know for interviews.
A helper recurses left, recurses right, then appends the node.
- Code: three lines that mirror the definition.
- Memory: the call stack holds one path.
- Limit: a very deep tree can hit the recursion limit.
Fine when recursion is allowed and the tree is shallow.
Both visit each node once in O(h) memory, but the stack version answers the follow-up and has no recursion-depth limit, so the steps, code and live demo follow it. The recursive code comes after the demo.
Walk left, pushing every node
From node, push it and move to node.left until node is empty. Each pushed node is waiting for both of its subtrees, and the left one comes first in postorder.
Check the top's right child
Look at the top of the stack without popping it. If it has a right child and that child is not last, the right subtree has not been visited: set node to the right child and go back to the first step. The parent stays on the stack, because it comes after that subtree.
Otherwise pop and record
With no right child, or a right child equal to last, both subtrees are finished. Pop the node, append its value and set last to it. When its parent reaches the top, this is how the parent sees that its right subtree is done.
Stop when both are empty
The loop runs while node or the stack is non-empty. When both are empty, every node has been pushed, popped and recorded, and the root, recorded last, closes the list.
Code in Python, C++ and Java
last to it. The next check looks at 2.last to it. The next check looks at 1.last to it. A subtree's root is always the last of its nodes to be recorded, which is how the check knew.last to it. The next check looks at 2.last to it. The next check looks at 5.last to it. The next check looks at 5.last to it. A subtree's root is always the last of its nodes to be recorded, which is how the check knew.last to it. A subtree's root is always the last of its nodes to be recorded, which is how the check knew.last to it. The next check looks at 3.last to it. A subtree's root is always the last of its nodes to be recorded, which is how the check knew.last to it. A subtree's root is always the last of its nodes to be recorded, which is how the check knew.node starts empty and the stack is empty, so the loop never runs and the empty answer is returned. No special case is needed.Recursive postorder traversal
dfs(node) returns at once on an empty node; otherwise it visits the left subtree, then the right subtree, and only then appends node.val. The call stack remembers which node to return to.
Common pitfalls
Checking only that a right child exists
if top.right:
node = top.rightif top.right and top.right is not last:
node = top.rightAfter the right subtree is recorded, the parent is on top again and its right child is still there. Without the last check the walk goes into the same subtree again and never stops.
Popping the node before its right subtree
top = stack.pop() result.append(top.val) node = top.right
top = stack[-1]
if top.right and top.right is not last:
node = top.right
else:
result.append(top.val)
last = stack.pop()Recording a node as soon as its left side is done gives inorder, not postorder. For [1,2,3] the answer comes out [2, 1, 3] instead of [2, 3, 1].
Complexity
Binary Tree Postorder Traversal FAQ
How does postorder traversal using two stacks work?
Run a preorder that visits node, right, left: pop a node from the first stack, push it onto the second, then push its left child and then its right child onto the first. When the first stack is empty, popping the second gives left, right, node, which is postorder. It is simpler to write but stores every node before any is output, so it uses O(n) space. With one list, append instead of pushing to the second stack and reverse it at the end.
Can it be done in O(1) extra space?
Yes, with Morris traversal. It threads the rightmost node of each left subtree back to its parent so it can climb up without a stack, and when it returns along a thread it outputs that left subtree's right edge in reverse. Time stays O(n) and the tree is restored, but the code is long and rarely asked for.