var largestValues = function(root) {
const list = []
// 广度优先遍历,记录每一层的最大值
const bfs = (root, count = 0) => {
if(!root) return
if(typeof list[count] === 'number') {
list[count] = Math.max(list[count], root.val)
} else {
list[count] = root.val
}
list[count] = Math.max(list[count] || 0, root.val)
bfs(root.left, count + 1)
bfs(root.right, count + 1)
}
bfs(root)
return list
};