Easy | C++ | O(N) fast
The naive approach is to iterate over the tree using any traversal and checking for each node the average of subtree.\n\nThe Optimal approach will be bottom up 
**The naive approach is to iterate over the tree using any traversal and checking for each node the average of subtree.**\n\n**The Optimal approach will be bottom up approach and returning count and sum of nodes in subtree using pair c++**\n\n```\nclass Solution {\npublic:\n    pair<int,int> iterate(TreeNode *root,int &ans)\n    {\n        if(!root)return {0,0};\n\t\t\n        pair<int,int>left=iterate(root->left,ans);\n        pair<int,int>right=iterate(root->right,ans);\n\t\t\n        int count=left.first+right.first+1;\n        int sum=left.second+right.second+root->val;\n\t\t\n        if(root->val==sum/count){\n            ans=ans+1;\n        }\n\t\t\n        return {count,sum};\n\n    }\n\t\n    int averageOfSubtree(TreeNode* root) \n\t{\n        int ans=0; \n        iterate(root,ans);\n        return ans;\n    }\n};\n```
[]