Sum, Count, Match
Our dfs function returns the sum and count of nodes, and the number of matching nodes.\n\nC++\ncpp\narray<int, 3> dfs(TreeNode* n) {\n    if (n == nullptr)\n   
Our `dfs` function returns the sum and count of nodes, and the number of matching nodes.\n\n**C++**\n```cpp\narray<int, 3> dfs(TreeNode* n) {\n    if (n == nullptr)\n        return {0, 0, 0};\n    auto p1 = dfs(n->left), p2 = dfs(n->right);\n    int sum = p1[0] + p2[0] + n->val, count = 1 + p1[1] + p2[1];\n    return {sum, count, p1[2] + p2[2] + (n->val == sum / count)};\n}\nint averageOfSubtree(TreeNode* root) {\n    return dfs(root)[2];\n}\n```
[]