1. var largestValues = function(root) {
    2. const list = []
    3. // 广度优先遍历,记录每一层的最大值
    4. const bfs = (root, count = 0) => {
    5. if(!root) return
    6. if(typeof list[count] === 'number') {
    7. list[count] = Math.max(list[count], root.val)
    8. } else {
    9. list[count] = root.val
    10. }
    11. list[count] = Math.max(list[count] || 0, root.val)
    12. bfs(root.left, count + 1)
    13. bfs(root.right, count + 1)
    14. }
    15. bfs(root)
    16. return list
    17. };