Average of Levels in Binary Tree

Easy

Description

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.

Examples

Input:root = [3,9,20,null,null,15,7]
Output:[3.0,14.5,11.0]
Explanation:

Level averages.

Input:root = [1,2,3,4,5,6,7]
Output:[1.0,2.5,5.5]
Explanation:

Level 0 has node [1], average = 1.0. Level 1 has nodes [2,3], average = (2+3)/2 = 2.5. Level 2 has nodes [4,5,6,7], average = (4+5+6+7)/4 = 5.5.

Input:root = [5]
Output:[5.0]
Explanation:

Single node tree with only the root node 5. There is only one level containing the value 5, so the average is 5.0.

Constraints

  • 1 ≤ nodes ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!