-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_145.java
More file actions
28 lines (23 loc) · 691 Bytes
/
Copy pathP_145.java
File metadata and controls
28 lines (23 loc) · 691 Bytes
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
package leetcode.hard;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import utils.DataStructures.TreeNode;
public class P_145 {
public List<Integer> postorderTraversal(TreeNode root) {
final LinkedList<Integer> res = new LinkedList<>();
final Deque<TreeNode> s = new LinkedList<>();
if (root != null) {
s.addFirst(root);
}
while (!s.isEmpty()) {
final TreeNode curr = s.removeFirst();
if (curr != null) {
res.addFirst(curr.val);
s.addFirst(curr.left);
s.addFirst(curr.right);
}
}
return res;
}
}