-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path103. 二叉树的锯齿形层序遍历.java
More file actions
34 lines (33 loc) · 902 Bytes
/
103. 二叉树的锯齿形层序遍历.java
File metadata and controls
34 lines (33 loc) · 902 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
33
34
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> a=new LinkedList();
if(root==null){
return a;
}
List<Integer> b;
Deque<TreeNode> c=new LinkedList<>();
c.add(root);
boolean flag=true;
while(c.size()>0){
b=new LinkedList();
int size = c.size();
for(int i=0;i<size;i++){
TreeNode pop = c.poll();
if(flag){
b.add(pop.val);
}else{
b.add(0, pop.val);
}
if(pop.left!=null){
c.add(pop.left);
}
if(pop.right!=null){
c.add(pop.right);
}
}
flag=!flag;
a.add(b);
}
return a;
}
}