MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / postorderTraversal

Method postorderTraversal

TreePostOrderIterative.java:3–21  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

1class 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) {

Callers

nothing calls this directly

Calls 4

pushMethod · 0.80
popMethod · 0.80
isEmptyMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected