-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path32.java
More file actions
69 lines (39 loc) · 1.06 KB
/
32.java
File metadata and controls
69 lines (39 loc) · 1.06 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
if(root==null)
{
return result;
}
List<Integer> list = new LinkedList<Integer>();
Deque<TreeNode> deque = new LinkedList<TreeNode>();
deque.offer(root);
TreeNode flag = root;
int k=1;
while(!deque.isEmpty())
{
TreeNode p =deque.poll();
list.add(p.val);
if(p.left!=null)
{
deque.offer(p.left);
}
if(p.right!=null)
{
deque.offer(p.right);
}
if(flag==p)
{
flag = deque.peekLast();
if(k%2==0)
{
Collections.reverse(list);
}
result.add(list);
list = new LinkedList<Integer>();
k++;
}
}
return result;
}
}