Skip to main content
LeetCode 637, Easy. Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

Given the root of a binary tree, return <em>the average value of the nodes on each level in the form of an array</em>. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.

Examples

Example 1
Example 2

Constraints

  • The number of nodes in the tree is in the range [1, 10<sup>4</sup>]
  • -2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026