✅✅Beats 100.00%of users with Java, C++, Python.✅✅
java []\nclass Solution {\n    private int ans;\n\n    public int averageOfSubtree(TreeNode root) {\n        ans = 0;\n        calculateAverage(root);\n        
```java []\nclass Solution {\n    private int ans;\n\n    public int averageOfSubtree(TreeNode root) {\n        ans = 0;\n        calculateAverage(root);\n        return ans;\n    }\n\n    private int[] calculateAverage(TreeNode node) {\n        if (node == null)\n            return new int[]{ 0, 0 };        \n\n        int[] leftSubtree = calculateAverage(node.left);\n        int[] rightSubtree = calculateAverage(node.right);\n\n        int sum = node.val + leftSubtree[0] + rightSubtree[0];\n        int count = 1 + leftSubtree[1] + rightSubtree[1];\n\n        if (sum / count == node.val) \n            ans++;\n\n        return new int[]{ sum, count };\n    }\n}\n```\n```python []\nclass Solution:\n    def __init__(self):\n        self.ans = 0\n\n    def averageOfSubtree(self, root):\n        self.ans = 0\n        self.calculate_average(root)\n        return self.ans\n\n    def calculate_average(self, node):\n        if not node:\n            return (0, 0)\n\n        left_subtree = self.calculate_average(node.left)\n        right_subtree = self.calculate_average(node.right)\n\n        sum = node.val + left_subtree[0] + right_subtree[0]\n        count = 1 + left_subtree[1] + right_subtree[1]\n\n        if sum // count == node.val:\n            self.ans += 1\n\n        return (sum, count)\n```\n```cpp []\nclass Solution {\nprivate:\n    int ans;\n\npublic:\n    int averageOfSubtree(TreeNode* root) {\n        ans = 0;\n        calculateAverage(root);\n        return ans;\n    }\n\n    pair<int, int> calculateAverage(TreeNode* node) {\n        if (node == nullptr) \n            return {0, 0};\n\n        pair<int, int> leftSubtree = calculateAverage(node->left);\n        pair<int, int> rightSubtree = calculateAverage(node->right);\n\n        int sum = node->val + leftSubtree.first + rightSubtree.first;\n        int count = 1 + leftSubtree.second + rightSubtree.second;\n        \n        if (sum / count == node->val)\n            ans++;\n\n        return {sum, count};\n    }\n};\n```\n```csharp []\npublic class Solution {\n    private int ans;\n\n    public int AverageOfSubtree(TreeNode root) {\n        ans = 0;\n        CalculateAverage(root);\n        return ans;\n    }\n\n    private (int, int) CalculateAverage(TreeNode node) {\n        if (node == null) return (0, 0);\n        var leftSubtree = CalculateAverage(node.left);\n        var rightSubtree = CalculateAverage(node.right);\n        int sum = node.val + leftSubtree.Item1 + rightSubtree.Item1;\n        int count = 1 + leftSubtree.Item2 + rightSubtree.Item2;\n        if (sum / count == node.val) ans++;\n        return (sum, count);\n    }\n}\n```\n# Intuition\nTo solve this problem, you can use a recursive function to traverse the binary tree and calculate the average for each subtree. Here\'s a solution for this problem:\n\n# Approach\nThe **AverageOfSubtree** function initializes the **ans** variable to store the result and then calls the **CalculateAverage** function to traverse the tree and calculate the average for each subtree.\n\nThe **CalculateAverage** function is a recursive function that calculates the sum and count of nodes in the subtree rooted at the current node. It then checks whether the current node\'s value is equal to the average of the subtree and increments the **ans** variable accordingly.\n\nThe **ans** variable keeps track of the number of nodes where the value is equal to the average of the values in its subtree.\n\n# Complexity\n- Time complexity:\nThe time complexity of this solution is **O(N)**, where N is the number of nodes in the binary tree. This is because the algorithm visits each node in the tree once, and the work done at each node is constant.\n\n- Space complexity:\nThe space complexity is **O(H)**, where H is the **height** of the binary tree. In the worst case, where the tree is completely unbalanced and degenerates into a linked list, the height H is equal to the number of nodes N, resulting in **O(N)** space complexity due to the recursive function calls on the call stack.\n\n![please upvote.png](https://assets.leetcode.com/users/images/54fa0acd-7175-450b-a5be-17ec9a770680_1698891797.6741934.png)\n\n
['C#']