```
public class Solution {
// Time complexity is O(2n).
// Space complexity is O(3n). If space is a constraint we can change the stack to a list
// and add each solution element at position 0; however, the insertions and shifting
// cost time.
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> workStack = new Stack<TreeNode>();
Stack<Integer> solStack = new Stack<Integer>();
List<Integer> solution = new ArrayList<Integer>();
workStack.push(root);
while(!workStack.isEmpty()) {
TreeNode node = workStack.pop();
if (node != null) {
solStack.push(node.val);
workStack.push(node.left);
workStack.push(node.right);
}
}
while(!solStack.isEmpty()) {
solution.add(solStack.pop());
}
return solution;
}
}
```