Java solution by traversal a tree.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n   class Pair{\n     int sum;\n     int num;\n     Pair(int sum,int num){\n           this.sum=sum;\n           this.num=num;\n     }\n   }\n   int count=0;\n   public Pair sol(TreeNode root){\n     if(root==null)return new Pair(0,0);\n     Pair left= sol(root.left);\n     Pair right=sol(root.right);\n     int sum1=root.val+left.sum+right.sum;\n     int num1=1+left.num+ right.num;\n    \n    if(root.val==sum1/num1)count++;\n    return new Pair(sum1,num1);\n   }\n    public int averageOfSubtree(TreeNode root) {\n        Pair left= sol(root);\n        return count;\n    }\n}\n```
['Java']