Given the root node root of a binary tree, return an array where the element at index i is the maximum value among all nodes on level i of the tree, with the root considered level 0. If the tree is empty, return an empty array.
Constraints:
The number of nodes in the tree is in the range [0,104].
−231≤ Node.val ≤231−1
Input: root = [1, 3, 2, 5, 3, null, 9]-TreeNode
Output: [1, 3, 9]-List[int]
Explanation: Level 0 nodes [1] have max 1, level 1 nodes [3, 2] have max 3, and level 2 nodes [5, 3, 9] have max 9.
Input: root = [1, 2, 3]-TreeNode
Output: [1, 3]-List[int]
Explanation: Level 0 nodes [1] have max 1, and level 1 nodes [2, 3] have max 3.
Input: root = []-TreeNode
Output: []-List[int]
Explanation: The tree is empty, so there are no levels to compute maximum values for.
Input: root = [-10, -20, -30, -5, null, -15, -40]-TreeNode
Output: [-10, -20, -5]-List[int]
Explanation: Level 0 nodes [-10] have max -10, level 1 nodes [-20, -30] have max -20, and level 2 nodes [-5, -15, -40] have max -5.
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: