给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
返回其层次遍历结果:
var levelOrder = function(root) {
if(!root) return[]//不能为空,否者报错
const q = [[root,0]]
const res = []
while(q.length) {
const [n,l] = q.shift()
if(!res[l]) {
res.push([n.val])
}else {
res[l].push(n.val)
}
if(n.left) q.push([n.left,l+1])
if(n.right) q.push([n.right,l+1])
}
return res
};