Java using Helper Function
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\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   \tstatic int count = 0;\n\n\tpublic static int averageOfSubtree(TreeNode root) {\n\t\tcount = 0;\n\t\thelper(root);\n\t\treturn count;\n\t}\n\n\tpublic static int helper(TreeNode root) {\n\t\tif (root == null)\n\t\t\treturn 0;\n\n\t\tint left = helper(root.left);\n\t\tint right = helper(root.right);\n\n\t\tint sum = root.val;\n\t\tif (root.left != null) {\n\t\t\tsum += root.left.val;\n\t\t}\n\t\tif (root.right != null) {\n\t\t\tsum += root.right.val;\n\t\t}\n\t\tif (sum / (left + right + 1) == root.val) {\n\t\t\tcount++;\n\t\t}\n\t\troot.val = sum;\n\t\treturn left + right + 1;\n\t}\n}\n```
['Java']