Given a binary tree, return the postorder traversal of its nodes' values.
Example:
Input:[1,null,2,3]
1 \ 2 / 3 Output:[3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
The core challenge of this problem is to traverse a binary tree in postorder, which means visiting the left subtree, then the right subtree, and finally the root node. This traversal is significant in various applications such as expression tree evaluations and syntax tree traversals in compilers.
Potential pitfalls include misunderstanding the traversal order and handling null nodes incorrectly.
To solve this problem, we can consider both recursive and iterative approaches:
The recursive approach is straightforward. We define a helper function that recursively visits the left subtree, right subtree, and then the root node.
The iterative approach is more complex but can be achieved using a stack. We use two stacks to simulate the postorder traversal. The first stack is used to traverse the tree, and the second stack is used to store the nodes in postorder.
// Definition for a binary tree node.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
postorderHelper(root, result);
return result;
}
private void postorderHelper(TreeNode node, List<Integer> result) {
if (node == null) {
return;
}
// Traverse left subtree
postorderHelper(node.left, result);
// Traverse right subtree
postorderHelper(node.right, result);
// Visit the root node
result.add(node.val);
}
}
import java.util.*;
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> result = new LinkedList<>();
if (root == null) {
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.addFirst(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
return result;
}
}
Both the recursive and iterative solutions have a time complexity of O(n), where n is the number of nodes in the tree. This is because each node is visited exactly once.
The space complexity for the recursive solution is O(h), where h is the height of the tree, due to the call stack. For the iterative solution, the space complexity is also O(h) due to the stack used for traversal.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
[]
[1]
[3, 2, 1]
[3, 2, 1]
[left subtree, right subtree, root]
When approaching tree traversal problems, it is helpful to:
Postorder tree traversal is a fundamental problem that helps in understanding tree data structures and their traversals. By practicing both recursive and iterative approaches, you can improve your problem-solving skills and be better prepared for similar problems.
For further reading and practice, consider the following resources: