0%

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

返回其层次遍历结果:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]

提示:

节点总数 <= 1000

BFS

和上一题差不多, 就是要用size()方法判断一下每层有多少个元素, 相同层的元素放到同一个list中

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null){
return new ArrayList<>();
}
List<List<Integer>> ans = new ArrayList<>();
Queue<TreeNode> q = new ArrayDeque<>();
q.offer(root);
while(!q.isEmpty()){
int size = q.size();
List<Integer> layerList = new ArrayList(size);
while(size-- != 0){
TreeNode curr = q.poll();
layerList.add(curr.val);
if(curr.left != null){
q.offer(curr.left);
}
if(curr.right != null){
q.offer(curr.right);
}
}
ans.add(layerList);
}
return ans;
}
}