-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103.java
More file actions
31 lines (23 loc) · 794 Bytes
/
103.java
File metadata and controls
31 lines (23 loc) · 794 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
29
30
31
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
boolean o = true;
while (!q.isEmpty()) {
int s = q.size();
LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < s; i++) {
TreeNode curr = q.poll();
if (o) list.addLast(curr.val);
else list.addFirst(curr.val);
if (curr.left != null) q.offer(curr.left);
if (curr.right != null) q.offer(curr.right);
}
res.add(list);
o = !o;
}
return res;
}
}