var largestValues = function (root) {
let res = [];
if (root === null) {
return res;
}
let q = [];
q.push(root);
while (q.length > 0) {
let sz = q.length;
// 记录这一层的最大值
let levelMax = -Infinity;
// 或者 let levelMax = Math.max();
// for 循环控制每一层从左向右遍历
for (let i = 0; i < sz; i++) {
let cur = q.shift();
levelMax = Math.max(levelMax, cur.val);
if (cur.left !== null) q.push(cur.left);
if (cur.right !== null) q.push(cur.right);
}
res.push(levelMax);
}
return res;
};