# 力扣之102. 二叉树的层序遍历

3
/ \
9  20
/  \
15   7

[
[3],
[9,20],
[15,7]
]

/**
* 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 = new Array();
let result = new Array();
result = [];
queue = [];
queue.push(root);
while (queue.length !== 0) {
result.push([]);
let currentLength = queue.length;
// console.log("outter",queue)
for (let i = 0; i < currentLength; i++) {
// console.log("innner",queue.length)
let node = queue.shift();
result[result.length - 1].push(node.val)
if (node.left) {
queue.push(node.left)
}
if (node.right) {
queue.push(node.right)
}
}
}
return result;
};

root的数据结构

TreeNode {
val: 3,
left: TreeNode 	{
val: 9,
left: null,
right: null
},
right: TreeNode {
val: 20,
left: TreeNode 	{
val: 15,
left: null,
right: null
},
right: TreeNode {
val: 7,
left: null,
right: null
}
}
}

queue的长度为1，2，4，8，16，32，64，128，256，…

09-06 21
10-26 1940

04-04 121
04-22 167
04-09 294
05-31
08-20