Problem
Section titled “Problem”Given the root of a binary tree, return the postorder traversal of its nodes’ values.
Example 1:

Input: root = [1,null,2,3]Output: [3,2,1]Example 2:

Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]Output: [4,6,7,5,2,9,8,3,1]Example 3:
Input: root = []Output: []Example 4:
Input: root = [1]Output: [1]Constraints:
- The number of the nodes in the tree is in the range
[0, 100]. -
Node.val
Follow-up: Recursive solution is trivial, could you do it iteratively?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Postorder outputs a node only after both of its subtrees. With an explicit stack we reach every node twice from below: once when we come back from its left subtree, and once when we come back from its right subtree. The difficulty is telling these two moments apart, because in both cases the node is on top of the stack.
Keep a pointer last to the most recently output node. When the top node node is examined:
- if it has a right child that is not
last, its right subtree has not been done yet — go into it; - otherwise (no right child, or we have just output that right child, which is always the last node of the right subtree in postorder) both subtrees are finished, so output
node.
Walking left before each examination (as in the iterative inorder traversal) guarantees the left subtree is always finished first.
Algorithm
Section titled “Algorithm”- Set
cur = root,last = nullptr. - While
curis not null orstis not empty:- while
curis not null, push it and movecur = cur->left; - let
node = st.top(); - if
node->rightexists andnode->right != last, setcur = node->right(descend into the right subtree); - otherwise pop
node, appendnode->val, and setlast = node(leavecurnull so we do not descend again).
- while
- Return
order.
Walkthrough
Section titled “Walkthrough”Example 1, root = [1,null,2,3] (stack shown bottom to top):
| Step | Stack | node | Decision | order |
|---|---|---|---|---|
push left spine of 1 | [1] | 1 | right child 2 not done: go right | [] |
push left spine of 2 | [1, 2, 3] | 3 | no right child: output | [3] |
| — | [1, 2] | 2 | no right child: output | [3, 2] |
| — | [1] | 1 | right child 2 == last: output | [3, 2, 1] |
Complexity
Section titled “Complexity”- Time: — every node is pushed once and popped once; the top is examined at most twice per node.
- Space: — the stack holds one root-to-node path, where is the tree height.
Edge cases
Section titled “Edge cases”- Empty tree (Example 3): the loop never runs and
[]is returned. - Right child just output: comparing
node->rightwithlastis what stops the loop from descending into the same right subtree forever.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> order; stack<TreeNode*> st; TreeNode *cur = root, *last = nullptr; while (cur || !st.empty()) { while (cur) { st.push(cur); cur = cur->left; } TreeNode* node = st.top(); if (node->right && node->right != last) { cur = node->right; } else { st.pop(); order.push_back(node->val); last = node; } } return order; }};class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> order = new ArrayList<>(); ArrayDeque<TreeNode> st = new ArrayDeque<>(); TreeNode cur = root, last = null; while (cur != null || !st.isEmpty()) { while (cur != null) { st.push(cur); cur = cur.left; } TreeNode node = st.peek(); if (node.right != null && node.right != last) { cur = node.right; } else { st.pop(); order.add(node.val); last = node; } } return order; }}class Solution: def postorderTraversal(self, root: TreeNode | None) -> list[int]: order = [] st = [] cur, last = root, None while cur or st: while cur: st.append(cur) cur = cur.left node = st[-1] if node.right and node.right is not last: cur = node.right else: st.pop() order.append(node.val) last = node return orderGive it a try first
Attempt the problem on LeetCode — the code will still be here.