> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Average of Levels in Binary Tree

> Tested Python solution for LeetCode 637 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 637, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 637   # by problem number
lcpy gen -s average_of_levels_in_binary_tree   # by problem name
```

## 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](https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg)

```
Input: root = [3,9,20,null,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].
```

![Example 2](https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg)

```
Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/average_of_levels_in_binary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/average_of_levels_in_binary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque

from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(w) where w is the maximum width of the tree
    def average_of_levels(self, root: TreeNode[int] | None) -> list[float]:
        if root is None:
            return []
        result: list[float] = []
        queue = deque([root])
        while queue:
            level_size = len(queue)
            level_sum = 0
            for _ in range(level_size):
                node = queue.popleft()
                level_sum += node.val
                if node.left is not None:
                    queue.append(node.left)
                if node.right is not None:
                    queue.append(node.right)
            result.append(level_sum / level_size)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(w) where w is the maximum width of the tree |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.