Ctrl K

Mean of Each Tree Level

Easy
You are given the root of a binary tree. For each level, first the root, then its children, then its grandchildren and so on, compute the mean of the node values on that level. Return the means from top to bottom. An answer is accepted if every number is within 10⁻⁵ of the exact value.
Example 1
root=tree_node([10, 4, 7, None, 1, -2, 8]) → [10.0, 5.5, 2.3333333333333335]

Level 0: 10. Level 1: (4 + 7) / 2 = 5.5. Level 2: (1 − 2 + 8) / 3 ≈ 2.33333.

Example 2
root=tree_node([6]) → [6.0]

A single node makes one level with mean 6.

Constraints
  • 1 ≤ number of nodes ≤ 10⁴
  • -2³¹ ≤ Node.val ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth EasyLeaf Path With a Given Total Easy
loading Python…
Input
root=tree_node([10, 4, 7, None, 1, -2, 8])
Expected
[10.0, 5.5, 2.3333333333333335]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.