| 1 | class Solution { |
| 2 | // Using 2 Stack |
| 3 | public List<Integer> postorderTraversal(TreeNode root) { |
| 4 | List<Integer> postOrder = new ArrayList<Integer>(); |
| 5 | if(root==null) return postOrder; |
| 6 | Stack<TreeNode> stack1 = new Stack<TreeNode>(); |
| 7 | Stack<TreeNode> stack2 = new Stack<TreeNode>(); |
| 8 | stack1.push(root); |
| 9 | while(!stack1.isEmpty()) |
| 10 | { |
| 11 | TreeNode temp = stack1.pop(); |
| 12 | if(temp.left!=null) stack1.push(temp.left); |
| 13 | if(temp.right!=null) stack1.push(temp.right); |
| 14 | stack2.push(temp); |
| 15 | } |
| 16 | while(!stack2.isEmpty()) |
| 17 | { |
| 18 | postOrder.add(stack2.pop().val); |
| 19 | } |
| 20 | return postOrder; |
| 21 | } |
| 22 | |
| 23 | // Using 1 Stack |
| 24 | public List<Integer> postorderTraversal(TreeNode root) { |