-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_zigzag.java
More file actions
37 lines (36 loc) · 1.14 KB
/
Copy pathbinary_tree_zigzag.java
File metadata and controls
37 lines (36 loc) · 1.14 KB
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
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root==null)
return res;
Queue<TreeNode> q = new LinkedList();
q.add(root);
ArrayList<Integer> root_list = new ArrayList();
root_list.add(root.val);
res.add(root_list);
while(!q.isEmpty()){
int size = q.size();
ArrayList<Integer> level_list = new ArrayList();
for(int i=0;i<size;i++){
TreeNode tmp = q.poll();
if(tmp.left!=null){
q.add(tmp.left);
level_list.add(tmp.left.val);
}
if(tmp.right!=null){
q.add(tmp.right);
level_list.add(tmp.right.val);
}
}
res.add(level_list);
}
for(int i=0;i<res.size();i++){
if(i%2==1){
Collections.reverse(res.get(i));
}
if(res.get(i).size()==0)
res.remove(i);
}
return res;
}
}