Find Largest Value in Each Tree Row LeetCode solution in JavaScript

1
var largestValues = function (root) {
2
if (!root) return [];
3

4
const op = [];
5
const Q = [[root, 1]];
6
while (Q.length) {
7
const [r, l] = Q.shift();
8

9
if (op.length < l) op.push(-Infinity);
10
op[l - 1] = Math.max(op[l - 1], r.val);
11

12
if (r.left) Q.push([r.left, l + 1]);
13
if (r.right) Q.push([r.right, l + 1]);
14
}
15
return op;
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0