Binary Tree Postorder Traversal
Easy
Topics
Given the root of a binary tree, return its postorder traversal (left, right, node) as a list of values.
Example 1
visited first
The small number above each node is its position in the output: left subtree, right subtree, node, giving
[3, 2, 1]. Children are finished before their parent, which is what any bottom-up computation needs. The iterative version swaps the call stack for an explicit one.Tree given in level-order (breadth-first);
null marks a missing child.Input: root = [1,null,2,3] Output: [3,2,1]
Example 2
visited first
Input: root = [1,2,3,4,5] Output: [4,5,2,3,1]
Constraints
- 0 <= number of nodes <= 100
- -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls
Intuition
Postorder visits the node after both subtrees: left, right, then node. It is the order you would use to safely delete a tree.
Approach
- Recurse left, then recurse right.
- Append the current node’s value last.
Dry Run
1
/ \
2 3
/ \
4 5
The descent is identical for all three traversals -- only the position of
res.append moves. Here it is last:
at 1: descend left
at 2: descend left
at 4: descend left, descend right, both null
at 4: VISIT
at 2: descend right
at 5: descend left, descend right, both null
at 5: VISIT
at 2: VISIT
at 1: descend right
at 3: descend left, descend right, both null
at 3: VISIT
at 1: VISIT
answer 4 5 2 3 1
The root is visited LAST, and node 2 is visited only after both 4 and 5. That is
the defining property: when a node is reached, every one of its descendants has
already been handled.
On this one tree all three orders are worth holding side by side, because the
call sequence above never changes:
preorder 1 2 4 5 3 append before descending
inorder 4 2 5 1 3 append between the two descents
postorder 4 5 2 3 1 append after both descents
Postorder is the one to reach for when a node's answer depends on its children's
answers -- freeing a tree, summing subtrees, measuring heights. The dependency
runs upward, so the visit has to come last.Complexity
- Time — O(n): each node visited once.
- Space — O(h): stack up to height h.
Common Pitfalls
- The visit comes after both recursive calls. Moving it above
dfs(node.right)gives inorder, not postorder --4 2 5 1 3where postorder is4 5 2 3 1. - Postorder is the traversal to reach for when a node's answer depends on its children's answers -- deleting a tree, computing subtree sums, sizing subtrees. That dependency is the reason the visit is last.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def postorderTraversal(self, root):
res = []
def dfs(node):
if not node:
return
dfs(node.left)
dfs(node.right)
res.append(node.val)
dfs(root)
return resimport java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int[] postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> st = new ArrayDeque<>();
if (root != null) st.push(root);
while (!st.isEmpty()) {
TreeNode n = st.pop();
res.add(n.val);
if (n.left != null) st.push(n.left);
if (n.right != null) st.push(n.right);
}
Collections.reverse(res);
int[] a = new int[res.size()];
for (int i = 0; i < a.length; i++) a[i] = res.get(i);
return a;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
function<void(TreeNode*)> dfs = [&](TreeNode* node) {
if (!node) return;
dfs(node->left);
dfs(node->right);
res.push_back(node->val);
};
dfs(root);
return res;
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number[]}
*/ var postorderTraversal = function(root) {
const res = [];
const dfs = node => {
if (!node) return;
dfs(node.left);
dfs(node.right);
res.push(node.val);
};
dfs(root);
return res;
};