-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path30.java
More file actions
45 lines (26 loc) · 724 Bytes
/
30.java
File metadata and controls
45 lines (26 loc) · 724 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
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int[] levelOrder(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<>();
if(root==null) return new int[]{};
queue.offer(root);
while(!queue.isEmpty())
{
TreeNode p = queue.poll();
list.add(p.val);
if(p.left!=null)
{
queue.offer(p.left);
}
if(p.right!=null)
{
queue.offer(p.right);
}
}
int[] res = new int[list.size()];
for(int i=0; i<res.length; i++) {
res[i] = list.get(i);
}
return res;
}
}