-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path145.java
37 lines (36 loc) · 1013 Bytes
/
145.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class TreeNodePlus {
int time;
TreeNode root;
TreeNodePlus(TreeNode r,int t){
time = t;
root = r;
}
}
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNodePlus> stack = new Stack<>();
stack.push(new TreeNodePlus(root,1));
List<Integer> res = new ArrayList<>();
while(!stack.isEmpty()){
TreeNodePlus temp = stack.pop();
if(temp.root==null) continue;
if(temp.time==1){
stack.push(new TreeNodePlus(temp.root,0));
stack.push(new TreeNodePlus(temp.root.right,1));
stack.push(new TreeNodePlus(temp.root.left,1));
}else{
res.add(temp.root.val);
}
}
return res;
}
}