Binary Tree Postorder Traversal (Recursive)
Binary Tree Postorder Traversal (Recursive) is a easy trees & bst problem solved with the recursion pattern.
The best approach, iterative (reverse of root-right-left), runs in O(n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from recursive dfs up.
Problem
Return the postorder traversal (left, right, node) of a binary tree.
Examples
Example 1
- Input
root = [4, 2, 6, 1, 3]- Output
[1, 3, 2, 6, 4]
Example 2
- Input
root = [1]- Output
[1]
Constraints
- The tree has
0to100nodes.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Recursive DFS | O(n) | O(h) |
| Iterative (reverse of root-right-left) | O(n) | O(n) |
1Recursive DFS
O(n)O(h)Visit both children, then record the node.
- if node == null return.
- postorder(left); postorder(right); add(node.val).
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
postorder(root, out);
return out;
}
private void postorder(TreeNode node, List<Integer> out) {
if (node == null) return;
postorder(node.left, out);
postorder(node.right, out);
out.add(node.val);
}
}class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
out: List[int] = []
self.postorder(root, out)
return out
def postorder(self, node: Optional[TreeNode], out: List[int]) -> None:
if node is None:
return
self.postorder(node.left, out)
self.postorder(node.right, out)
out.append(node.val)class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> out;
postorder(root, out);
return out;
}
private:
void postorder(TreeNode* node, vector<int>& out) {
if (node == nullptr) return;
postorder(node->left, out);
postorder(node->right, out);
out.push_back(node->val);
}
};function postorderTraversal(root) {
const out = [];
postorder(root, out);
return out;
}
function postorder(node, out) {
if (node === null) return;
postorder(node.left, out);
postorder(node.right, out);
out.push(node.val);
}2Iterative (reverse of root-right-left)
O(n)O(n)A root, right, left preorder is exactly postorder reversed. Run that with a stack and add each value to the front of the list.
- Push root; pop, addFirst(val), push left, push right.
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> out = new LinkedList<>();
if (root == null) return out;
Deque<TreeNode> st = new ArrayDeque<>();
st.push(root);
while (!st.isEmpty()) {
TreeNode n = st.pop();
out.addFirst(n.val);
if (n.left != null) st.push(n.left);
if (n.right != null) st.push(n.right);
}
return out;
}
}class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
out: Deque[int] = deque()
if root is None:
return list(out)
st = [root]
while st:
n = st.pop()
out.appendleft(n.val)
if n.left is not None:
st.append(n.left)
if n.right is not None:
st.append(n.right)
return list(out)class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
deque<int> out;
if (root == nullptr) return vector<int>();
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* n = st.top(); st.pop();
out.push_front(n->val);
if (n->left != nullptr) st.push(n->left);
if (n->right != nullptr) st.push(n->right);
}
return vector<int>(out.begin(), out.end());
}
};function postorderTraversal(root) {
const out = [];
if (root === null) return out;
const st = [root];
while (st.length > 0) {
const n = st.pop();
out.unshift(n.val);
if (n.left !== null) st.push(n.left);
if (n.right !== null) st.push(n.right);
}
return out;
}Edge cases to test
- Empty tree
Hints
Hint 1
Postorder = left, right, then node. A node is handled only after both its subtrees, which is why height, diameter and balance checks use it.
FAQ
What is the best time complexity for Binary Tree Postorder Traversal (Recursive)?
Iterative (reverse of root-right-left) runs in O(n) time and O(n) extra space.
Which pattern does Binary Tree Postorder Traversal (Recursive) use?
It is a trees & bst problem that uses the recursion pattern. Other problems with the same pattern: Binary Tree Inorder Traversal (Recursive), Binary Tree Preorder Traversal (Recursive), Binary Tree Inorder Traversal (Iterative).
Is there a brute force solution for Binary Tree Postorder Traversal (Recursive)?
Yes. Recursive DFS takes O(n) time and O(h) space. Visit both children, then record the node.
Which edge cases should I test for Binary Tree Postorder Traversal (Recursive)?
Empty tree.