var levelOrder = function(root) {
if (!root) return [];
let queue = [];
let res = [];
let level = 0;
queue.push(root);
let temp;
while (queue.length) {
res.push([]);
let size = queue.length; // 注意一下:
size--; //在层次遍历中是一个非常重要的技巧
while (size--) {
// 出队
let front = queue.shift();
res[level].push(front.val); // 入队
if (front.left) queue.push(front.left);
if (front.right) queue.push(front.right);
}
level++;
}
return res;
};
02-01
2544