Whole-Number Average of Every Level
Implement levelAverages
You are given the root of a binary tree whose node values are non-negative integers. For every level of the tree, from the root level down, compute the average of the values on that level and round it down to a whole number. Return these averages as a list, one per level.
Collecting the values of every level first works. Going level by level with a queue lets you keep just one running sum at a time.
Example 1:
Input: root = [12,4,20,7,3,null,10,9]
Output: [12,12,6,9]
Example 2:
Input: root = [5,1,2]
Output: [5,1]
Example 3:
Input: root = []
Output: []
+ 11 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100 - ●
0 ≤ node.val ≤ 1000 (values are never negative, so rounding down is unambiguous) - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
For each level of the tree, from the root level downwards, return the average of its values ROUNDED DOWN to a whole number (for example an average of 6.67 becomes 6). An empty tree gives an empty list
root =
[12, 4, 20, 7, 3, null, 10, 9]