-
Notifications
You must be signed in to change notification settings - Fork 16
/
BinaryTreeZigzagLevelOrderTraversal.java
50 lines (42 loc) · 1.45 KB
/
BinaryTreeZigzagLevelOrderTraversal.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal
// T: O(n)
// S: O(n)
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BinaryTreeZigzagLevelOrderTraversal {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) return Collections.EMPTY_LIST;
final List<List<Integer>> result = new ArrayList<>();
final Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
queue.add(null);
List<Integer> level = new ArrayList<>();
while (!queue.isEmpty()) {
final TreeNode current = queue.poll();
if (current == null) {
result.add(level);
level = new LinkedList<>();
if (!queue.isEmpty()) {
queue.add(null);
}
continue;
}
level.add(current.val);
addChildrenInQueue(queue, current);
}
zigZagOrder(result);
return result;
}
private void zigZagOrder(List<List<Integer>> levels) {
for (int i = 1 ; i < levels.size() ; i += 2) {
Collections.reverse(levels.get(i));
}
}
private void addChildrenInQueue(Queue<TreeNode> queue, TreeNode root) {
if (root.left != null) queue.add(root.left);
if (root.right != null) queue.add(root.right);
}
}