✅ [Java/C++] 100% Solution using Depth-First Search (Count Nodes Equal to Average of Subtree)
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n\n# Co
# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int cnt = 0;\n    pair<int,int> solve(TreeNode* root)\n    {\n        if(root == NULL)\n            return {0,0};\n        auto left = solve(root->left);\n        auto right = solve(root->right);\n        int sum = left.first + right.first + root->val;\n        int nodes = left.second + right.second + 1;\n        if(sum/nodes == root->val)\n            cnt++;\n        return {sum,nodes};\n    }\n    int averageOfSubtree(TreeNode* root) {\n        solve(root);\n        return cnt;\n    }\n};\n```\n```Java []\nclass Solution {\n    int cnt = 0;\n    int[] solve(TreeNode root)\n    {\n        if(root == null)\n            return new int[]{0,0};\n        int[] left = solve(root.left);\n        int[] right = solve(root.right);\n        int sum = left[0] + right[0] + root.val;\n        int nodes = left[1] + right[1] + 1;\n        if(sum/nodes == root.val)\n            cnt++;\n        return new  int[]{sum, nodes};\n    }\n    public int averageOfSubtree(TreeNode root) {\n        solve(root);\n        return cnt;\n    }\n}\n```
['Tree', 'Depth-First Search', 'Binary Tree', 'C++', 'Java']