Simple recursion with memory saving by passing a pair
\n# 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# 
\n# 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```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    \n    pair<int,int> solve(TreeNode * root,int &count){\n        if(root==NULL)\n            return {0,0};\n        \n        auto left=solve(root->left,count);\n        auto right=solve(root->right,count);\n        \n        if(((root->val+left.first+right.first)/(1+left.second+right.second))==root->val)\n            count+=1;\n        \n        return {root->val+left.first+right.first,1+left.second+right.second};\n            \n    }\n    int averageOfSubtree(TreeNode* root) {\n        \n        int count=0;\n        \n        solve(root,count);\n        \n        return count;\n        \n    }\n};\n```
['Recursion', 'Binary Tree', 'C++']