-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (28 loc) · 800 Bytes
/
Copy pathSolution.java
File metadata and controls
32 lines (28 loc) · 800 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
32
import java.util.*;
public class Solution
{
public List<List<Integer>> zigzagLevelOrder(TreeNode root)
{
List<List<Integer>> result = new ArrayList<>();
zigzagLevelOrder(result, root, 0);
for (int i = 1; i < result.size(); i = i + 2)
{
Collections.reverse(result.get(i));
}
return result;
}
private void zigzagLevelOrder(List<List<Integer>> result, TreeNode root, int height)
{
if (root == null)
{
return;
}
if (height == result.size())
{
result.add(new LinkedList<Integer>());
}
result.get(height).add(root.val);
zigzagLevelOrder(result, root.left, height + 1);
zigzagLevelOrder(result, root.right, height + 1);
}
}