-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_144.java
More file actions
28 lines (24 loc) · 723 Bytes
/
Copy pathP_144.java
File metadata and controls
28 lines (24 loc) · 723 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.medium;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import utils.DataStructures.TreeNode;
public class P_144 {
public List<Integer> preorderTraversal(TreeNode root) {
final List<Integer> res = new ArrayList<>();
final Deque<TreeNode> dq = new LinkedList<>();
TreeNode curr = root;
while (!dq.isEmpty() || curr != null) {
if (curr != null) {
dq.addFirst(curr);
res.add(curr.val);
curr = curr.left;
} else {
curr = dq.removeFirst();
curr = curr.right;
}
}
return res;
}
}