Java | 1ms | Beats 99% runtime| Recursive Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nIn general, average of values = (sum of all values / total no of values).\nIn order to 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn general, average of values = (sum of all values / total no of values).\nIn order to find the average of subtree for a certain TreeNode, we need to find the sum of all nodes in its left subtree, sum of all nodes in its right subtree, and the total number of nodes in both left and right subtree. This information will be enough to check the condition for the given node.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe create a function that returns an array with 2 integers in it.\nint[ ] arr = {`sum of all nodes in subtree`, `number of nodes in subtree`}\nFor every TreeNode we also check the condition whether the value of the node is equal to the average of the values in its subtree. If the condition is true, we increment the `ans` variable, which was initialised to 0.\n\n# Code\n```\nclass Solution {\n    int ans = 0;\n\n    public int averageOfSubtree(TreeNode root) {\n        subtreeSum(root);\n        return ans;\n    }\n\n    // function to calculate sum of all nodes in subtree\n    private int[] subtreeSum(TreeNode root){\n        // base condition\n        if(root == null)\n            return new int[]{0, 0};\n\n        // for leaf nodes\n        if(root.left == null && root.right == null){ \n            ans++;\n            return new int[]{root.val, 1};\n        }\n\n        int[] leftSum = subtreeSum(root.left);\n        int[] rightSum = subtreeSum(root.right);\n        if((leftSum[0] + rightSum[0] + root.val) / (leftSum[1] + rightSum[1] + 1) == root.val)\n            ans++;\n\n        return new int[] {(leftSum[0] + rightSum[0] + root.val), (leftSum[1] + rightSum[1] + 1)};\n    }\n}\n```
['Depth-First Search', 'Recursion', 'Java']