Algo-Sim › Binary Trees & BST
Postorder Traversal (Recursive)
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Recursive depth-first traversal that outputs each node after both subtrees.
Problem
Given the root of a binary tree, return its values in postorder: the left subtree, then the right subtree, then the node itself. This version recurses.
Constraints
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
- A node is output only after both of its subtrees
Edge cases to test
- Empty tree:
[] - A node with only a right child
- One node
Follow-up questions
- Can you do it without recursion?
- One stack plus the last node output, or a node–right–left preorder reversed at the end (two stacks): O(n).
- When is postorder the natural order?
- Whenever a node needs its children’s answers first: heights, subtree sums, deleting or freeing a tree.
Solution
Java
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
visit(root, out);
return out;
}
private void visit(TreeNode node, List<Integer> out) {
if (node == null) return;
visit(node.left, out);
visit(node.right, out);
out.add(node.val);
}Python
def postorder_traversal(root):
out = []
def visit(node):
if not node:
return
visit(node.left)
visit(node.right)
out.append(node.val)
visit(root)
return outC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> out;
visit(root, out);
return out;
}
void visit(TreeNode *node, vector<int> &out) {
if (!node)
return;
visit(node->left, out);
visit(node->right, out);
out.push_back(node->val);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)