C++ solution || O(N) || 100.00% faster and easy
Approach\nRecursion using pair\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n\nclass Solution {\npublic:\n  // Used pair to 
# Approach\nRecursion using pair\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\npublic:\n  // Used pair to store sum and count for every subtree\n  typedef pair<int, int> pr;\n  pr find(TreeNode* root, int &res) {\n    if(!root) return {0, 0};\n    pr left = find(root->left, res);\n    pr right = find(root->right, res);\n\n    int sum = root->val + left.first + right.first;\n    int count = left.second+right.second+1; \n    if(root->val == sum / count) res++;\n    return {sum, count};\n   }\n  int averageOfSubtree(TreeNode* root) {\n    int res = 0; \n    find(root, res);\n    return res;  \n  }\n};\n```
['C++']