function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
var levelOrder = function(root) {
let res = [];
if(!root) return res;
let queue = [root];
while(queue.length){
let size = queue.length;
let level = [];
for(let i=0; i<size;i++){
let node = queue.shift();
level.push(node.val);
if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
res.push(level);
}
return res;
};
网友评论