CPP | Short and Simple | O(n) Time | DFS | O(1) Space
\nclass Solution {\npublic:\n    pair<int,int> func(TreeNode* root,int &ans){\n        if(!root)return {0,0};\n        auto p1=func(root->left,ans);\n        au
```\nclass Solution {\npublic:\n    pair<int,int> func(TreeNode* root,int &ans){\n        if(!root)return {0,0};\n        auto p1=func(root->left,ans);\n        auto p2=func(root->right,ans);\n        int avg=(root->val+p1.first+p2.first)/(p1.second+p2.second+1);\n        if(avg==root->val)ans++;\n        return {root->val+p1.first+p2.first,p1.second+p2.second+1};\n    }\n    int averageOfSubtree(TreeNode* root) {\n        int ans=0;\n        func(root,ans);\n        return ans;\n    }\n};\n```
['Tree', 'C++']