给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)
3
/ \
9 20
/ \
15 7
[
[3],
[9,20],
[15,7]
]
//先利用广度优先遍历var levelOrder = function(root) {if (!root) return []const q = [root]while(q.length){const n = q.shift()console.log(n.val)if(n.left) q.push(left)if(n.right) q.push(right)}}
//记录层级var levelOrder = function(root) {if (!root) return []const q = [[root, 0]]while(q.length){const [n,level] = q.shift()console.log(n.val, l)if(n.left) q.push([n.left, level+1])if(n.right) q.push([n.right, level+1])}}//push 数组里var levelOrder = function(root) {if (!root) return []const q = [[root, 0]]let res = []while(q.length){const [n,level] = q.shift()if(!res[level]){res.push([n.val])}else{res[level].push(n.val)}if(n.left) q.push([n.left, level+1])if(n.right) q.push([n.right, level+1])}return res}//方法二var levelOrder = function(root) {if (!root) return []const q = [root]const res = []while(q.length){let len = q.length;res.push([]);while(len--){const n = q.shift()res[res.length-1].push(n.val)if(n.left) q.push(n.left)if(n.right) q.push(n.right)}}return res}
