Loading problem...
Given the root node of a binary tree, compute and return an array containing the maximum (largest) value found at each level of the tree.
The levels are defined from top to bottom, with the root node being at level 0 (the first level). For each level in the tree, identify the node with the highest value and include that value in the result array at the corresponding index.
If the tree is empty (root is null), return an empty array.
Binary Tree Representation:
The binary tree is represented in level-order traversal format, where null indicates the absence of a node at that position. For example, [1,3,2,5,3,null,9] represents:
1 ← Level 0 (max = 1)
/ \
3 2 ← Level 1 (max = 3)
/ \ \
5 3 9 ← Level 2 (max = 9)
root = [1,3,2,5,3,null,9][1,3,9]The binary tree has 3 levels. At level 0, the only node is 1, so max = 1. At level 1, nodes are 3 and 2, so max = 3. At level 2, nodes are 5, 3, and 9, so max = 9. The result array is [1, 3, 9].
root = [1,2,3][1,3]Level 0 contains only the root node with value 1. Level 1 contains nodes with values 2 and 3. The maximum at level 0 is 1, and the maximum at level 1 is 3.
root = [5][5]A single-node tree has only one level. The root node value 5 is the maximum (and only) value at level 0.
Constraints