# 1120. Maximum Average Subtree

{% embed url="<https://leetcode.com/problems/maximum-average-subtree/>" %}

递归遍历，传递当前sum和node count，全局变量记录maxMean。

```python
class Solution:
    def __init__(self):
        self.maxMean = 0
        
    def maximumAverageSubtree(self, root: TreeNode) -> float:
        self.traversal(root)
        return self.maxMean
    
    def traversal(self, root):
        if not root: return (0, 0)
        sumLeft, countLeft = self.traversal(root.left)
        sumRight, countRight = self.traversal(root.right)
        sum_ = sumLeft + root.val + sumRight
        count = countLeft + 1 + countRight
        self.maxMean = max(self.maxMean, sum_/count)
        return (sum_, count)
```
