Skip to main content
Back to problems
Leetcode
Medium
Trees
Queues
Arrays
Amazon
Microsoft
Average Of Levels In Binary Tree

Compute the average value of the nodes on each level of a binary tree.

Acceptance 0%
Problem Statement

Given the root of a binary tree, return the average value of the nodes on each level, from top to bottom.

Traverse the tree level by level, and for each level compute the arithmetic mean of all node values on that level.

Input Format

  • A binary tree root node is provided as the input.
  • Each node contains an integer value and optional left/right children.

Output Format

  • Return a list of floating-point numbers.
  • The ii-th value is the average of all node values on level ii of the tree, starting from level 0 at the root.

Constraints

  • The tree may be empty.
  • Node values fit in 32-bit signed integers.
  • The number of nodes is finite.
  • Use floating-point division for each level average.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

[3.0,14.5,11.0]

Explanation

Level 0: [3] -> 3.0 Level 1: [9,20] -> (9 + 20) / 2 = 14.5 Level 2: [15,7] -> (15 + 7) / 2 = 11.0

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.