Average of Levels in Binary Tree

IF
AlgoAxiomStaff Engineers
JSTS
Easy20 mins

Given the root of a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10⁻⁵ of the actual answer will be accepted.

Examples

Example 1:

3920157

Input: root = [3, 9, 20, null, null, 15, 7]

Output: [3, 14.5, 11]

Explanation: Level 0: average of [3] = 3. Level 1: average of [9, 20] = 14.5. Level 2: average of [15, 7] = 11.

Example 2:

3920157

Input: root = [3, 9, 20, 15, 7]

Output: [3, 14.5, 11]

Example 3:

1

Input: root = [1]

Output: [1]

Constraints

  • The number of nodes in the tree is in the range [1, 10⁴]
  • -2³¹ <= Node.val <= 2³¹ - 1
Source: Tree Breadth-First Search pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle