Skip to content

Binary Tree Postorder Traversal

Given the root of a binary tree, return the postorder traversal of its nodes’ values.

Example 1:

Tree 1 with right child 2, which has left child 3

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

Example 2:

Tree for 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].
  • −100≤-100 \le Node.val ≤100\le 100

Follow-up: Recursive solution is trivial, could you do it iteratively?

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.

  1. Set cur = root, last = nullptr.
  2. While cur is not null or st is not empty:
    • while cur is not null, push it and move cur = cur->left;
    • let node = st.top();
    • if node->right exists and node->right != last, set cur = node->right (descend into the right subtree);
    • otherwise pop node, append node->val, and set last = node (leave cur null so we do not descend again).
  3. Return order.

Example 1, root = [1,null,2,3] (stack shown bottom to top):

StepStacknodeDecisionorder
push left spine of 1[1]1right child 2 not done: go right[]
push left spine of 2[1, 2, 3]3no right child: output[3]
—[1, 2]2no right child: output[3, 2]
—[1]1right child 2 == last: output[3, 2, 1]
  • Time: O(n)O(n) — every node is pushed once and popped once; the top is examined at most twice per node.
  • Space: O(h)O(h) — the stack holds one root-to-node path, where hh is the tree height.
  • Empty tree (Example 3): the loop never runs and [] is returned.
  • Right child just output: comparing node->right with last is 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 order

Give it a try first

Attempt the problem on LeetCode — the code will still be here.