# 二叉树遍历
# 广度优先遍历
# 层次遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/* 层次遍历 */
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
if (!root) return [];
let queue = [root]; // 队列
let res = []; // 保存每层结果
while (queue.length) {
let levelSize = queue.length; // 当前层的长度
res.push([]);
for (let i = 0; i < levelSize; ++i) {
let node = queue.shift(); // 出队
// 把当前层的每个节点的左右子节点入队
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
res[res.length - 1].push(node.val);
}
}
return res;
};
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
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