Binary Tree Postorder Traversal
Problem Statement:β
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]
Explanation:

-
Example:
β Solution 1: Recursive Postorder Traversalβ
class Solution {
public:
void postOrd(TreeNode* root, vector<int> &res){
if(root == NULL) return;
postOrd(root->left, res); // Left
postOrd(root->right, res); // Right
res.push_back(root->val); // Root
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
postOrd(root, res);
return res;
}
};
β Solution 2: Iterative Postorder Using Two Stacksβ
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL) return res;
stack<TreeNode*> st1, st2;
st1.push(root);
while(!st1.empty()){
root = st1.top(); st1.pop();
st2.push(root);
if(root->left) st1.push(root->left);
if(root->right) st1.push(root->right);
}
while(!st2.empty()){
res.push_back(st2.top()->val);
st2.pop();
}
return res;
}
};
β Solution 3: Iterative Postorder Using One Stackβ
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL) return res;
stack<TreeNode*> st;
TreeNode* curr = root;
while(curr != NULL || !st.empty()) {
while(curr != NULL) {
if(curr->right) st.push(curr->right); // Push right child first
st.push(curr); // Push current node
curr = curr->left; // Move to left child
}
curr = st.top(); st.pop();
// Check if the right child is next in stack
if(!st.empty() && curr->right != NULL && curr->right == st.top()) {
st.pop(); // Remove right child
st.push(curr); // Push root back for later processing
curr = curr->right; // Process right child next
} else {
res.push_back(curr->val);
curr = NULL;
}
}
return res;
}
};
π How It Worksβ
Recursive:β
- Simple and natural: Traverse left, then right, then visit root.
Two Stacks:β
- First stack processes nodes in
Root β Right β Left. - Second stack reverses this to
Left β Right β Root.
One Stack:β
- Tricky but optimized.
- Use one stack to simulate the recursion with careful checks to delay visiting root until after both subtrees are processed.
π§© Key Order Ruleβ
Postorder Traversal = [ Left, Right, Root ]
β±οΈ Time & Space Complexityβ
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Recursive | O(n) | O(h) recursion stack |
| Two-stack | O(n) | O(n) |
| One-stack | O(n) | O(n) (worst-case skewed) |
β οΈ Edge Casesβ
- β Empty tree β returns empty vector
- β Tree with one node β returns single-element vector
- β Only left or only right skewed trees β still works
- β Balanced trees β handles recursion depth and stack growth efficiently
π‘ Other Approachesβ
| Approach | Notes |
|---|---|
| Morris Postorder Traversal | O(1) space, very tricky and not common in interviews |
| Marked-Node Stack | Push command/state with node, similar to iterative simulation of recursion |
π Related Problemsβ
- LeetCode 145. Binary Tree Postorder Traversal
- LeetCode 144. Preorder Traversal
- LeetCode 94. Inorder Traversal
- LeetCode 102. Level Order Traversal
π¬