> ## 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.

# Maximum Average Subtree Python Solution

> Tested Python solution for LeetCode 1120 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 1120, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/maximum-average-subtree/description/).

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

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

## Problem

Given the `root` of a binary tree, return *the maximum **average** value of a subtree of that tree*. Answers within `10^-5` of the actual answer will be accepted.

A **subtree** of a tree is any node of that tree plus all its descendants.

The **average** value of a tree is the sum of its values, divided by the number of nodes.

### Examples

```
Input: root = [5,6,1]
Output: 6.00000
Explanation: For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4. For the node with value = 6 we have an average of 6 / 1 = 6. So the answer is 6 which is the maximum.
```

```
Input: root = [0,null,1]
Output: 1.00000
```

### Constraints

* The number of nodes in the tree is in the range \[1, 10^4].
* 0 \<= Node.val \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_average_subtree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_average_subtree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h) for the recursion stack
    def maximum_average_subtree(self, root: TreeNode[int] | None) -> float:
        ans = 0.0

        def dfs(node: TreeNode[int] | None) -> tuple[int, int]:
            nonlocal ans
            if node is None:
                return 0, 0
            left_sum, left_n = dfs(node.left)
            right_sum, right_n = dfs(node.right)
            total = node.val + left_sum + right_sum
            count = 1 + left_n + right_n
            ans = max(ans, total / count)
            return total, count

        dfs(root)
        return ans
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) for the recursion stack |

## Tags

[NeetCode All](/catalog/neetcode).


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