# Count Nodes Equal to Average of Subtree (Leetcode 2265)

Problem Link: <https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/>

```python
class Solution:
    def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
        
        global ans
        ans=0
        
        def fun(root):
        
            global ans
            if(root is None):
                return [0,0]
            
            l=fun(root.left)
            r=fun(root.right)
            if((l[0]+r[0]+root.val)//(l[1]+r[1]+1) == root.val):
                ans=ans+1
            return [l[0]+r[0]+root.val,l[1]+r[1]+1]
        
        fun(root)
        return ans
```
