Leetcoe 515 - Find largest value in each row

Note

  • Use level order traversal

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
Example

img

1
2
Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var largestValues = function(root) {
let result = [];
if (!root) return result;
let queue = [root];

while (queue.length) {
let length = queue.length;
let biggest = -(2**31) - 1;
for (let i = 0; i < length; i++) {
let node = queue.shift();
if (node.val > biggest) biggest = node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
result.push(biggest);
}
return result;
};