Find Largest Value in Each Tree Row LeetCode solution in JavaScript
1var largestValues = function (root) {2if (!root) return [];34const op = [];5const Q = [[root, 1]];6while (Q.length) {7const [r, l] = Q.shift();89if (op.length < l) op.push(-Infinity);10op[l - 1] = Math.max(op[l - 1], r.val);1112if (r.left) Q.push([r.left, l + 1]);13if (r.right) Q.push([r.right, l + 1]);14}15return op;16};