Skip to main content

0145 - Binary Tree Postorder Traversal (Easy)

https://leetcode.com/problems/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]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

Constraints:

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

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

Approach 1: DFS - Post-order traversal

Written by @wingkwong
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/

// Time Complexity: O(N)
// Space Complexity: O(N)

// This is a standard post-order traversal problem, I'd suggest to learn in-order and pre-order as well.
// Here's a short tutorial if you're interested.
// https://wingkwong.github.io/leetcode-the-hard-way/tutorials/graph-theory/binary-tree
// then you may try the following problems
// 94. Binary Tree Inorder Traversal: https://leetcode.com/problems/binary-tree-inorder-traversal/
// 144. Binary Tree Postorder Traversal: https://leetcode.com/problems/binary-tree-preorder-traversal/

class Solution {
public:
vector<int> ans;
void postoder(TreeNode* node) {
if (node == NULL) return;
postoder(node->left);
// traverse the left node
postoder(node->right);
// traverse the right node
ans.push_back(node->val);
// do something with node value here
}

vector<int> postorderTraversal(TreeNode* root) {
postoder(root);
return ans;
}
};

Approach 2: Iterative

Written by @vigneshshiv
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
// Time complexity: O(n), where n - # of nodes in tree
// Space complexity: O(n)
class Solution {
/**
* Sample binary tree
*
* 1
* / \
* 2 3
*
*/
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
// Reference for last visited right node, for when parent is on top of the stack
TreeNode last = null;
while (root != null || !stack.isEmpty()) {
// Keep pushing left nodes, all the way down onto stack
if (root != null) {
stack.push(root);
root = root.left;
} else {
TreeNode node = stack.peek();
// When Parent is on top stack, it checks with right node which has a refence in last variable
// If both are same, it will not add repeated reference onto stack
// Pops out stack top, i.e parent node, and level up higher for other nodes.
if (node.right != null && node.right != last) {
root = node.right;
} else {
// If any of the right node is empty, the block executes and add value from top of stack
result.add(node.val);
// Pops out stock top
last = stack.pop();
}
}
}
return result;
}
}