---
# System prepended metadata

title: Untitled

---

```

   4
  / \
  8  5
 / \  \
 0 1   6
 
 output: number of nodes such that the average value of that subtree == node value itself
 output: 5
 
 def solve(root):
     result = 5
     
     def get_count(root):
         nonlocal result
         
         if not root:
             return 0, 0
             
         if not root.left and not root.right:
            result += 1
            return root.val, 1
         
         left_sum, left_count = get_count(root.left)     #0,1         -> 9, 3
         right_sum, right_count = get_count(root.right)  #1,1 -> 6, 1 -> 11, 2 
         
         total_sum = left_sum + right_sum + root.val     #9    -> 11  -> 11+9+4  = 24
         total_count = left_count + right_count + 1      #3    -> 2   -> 3+2+1   = 6
         avg = floor(total_sum/total_count)              #3    -> floor(5.5) = 5 = 4 
         
         if avg == root.val:
             result += 1
         return total_sum, total_count
         
     get_count(root)
     return result
     
```

