-
Notifications
You must be signed in to change notification settings - Fork 138
/
lc102.java
44 lines (42 loc) · 1.18 KB
/
lc102.java
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
package code;
/*
* 102. Binary Tree Level Order Traversal
* 题意:二叉树层次遍历
* 难度:Medium
* 分类:Tree, Breadth-first Search
* 思路:用一个队列
* Tips:如何判断遍历完一层了?pop的时候先看下size
*/
import java.util.*;
public class lc102 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> qu = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if(root==null)
return res;
qu.add(root);
while(!qu.isEmpty()){ //两个while
int size = qu.size();
List<Integer> temp = new ArrayList<>();
while(size>0){
TreeNode tn = qu.remove();
if(tn!=null) {
if(tn.left!=null)
qu.add(tn.left);
if(tn.right!=null)
qu.add(tn.right);
}
temp.add(tn.val);
size--;
}
res.add(temp);
}
return res;
}
}