C++ | 2 Approaches | BFS | DFS  | Easy to understand
Please upvote it you find this solution helpful:)\nApproach-1: Use Level Order traversal / BFS\n\nclass Solution {\npublic:\n    \n    \n    //use level order t
**Please upvote it you find this solution helpful:)**\n**Approach-1: Use Level Order traversal / BFS**\n```\nclass Solution {\npublic:\n    \n    \n    //use level order traversal and check whether average of the subtree is equal to the node value, if it is increase the res by 1.\n    void help(TreeNode* node, int &res)\n    {\n        int sum=0, cnt=0;\n        \n        queue<TreeNode*> q;\n        q.push(node);\n        while(!q.empty())\n        {\n            int n = q.size();\n            for(int i=0; i<n; i++)\n            {\n                TreeNode* nod = q.front();\n                q.pop();\n                sum += nod->val;\n                cnt++;\n                if(nod->left) q.push(nod->left);\n                if(nod->right) q.push(nod->right);\n            }\n        }\n        if(sum/cnt == node->val) //if average equals to the node value increase res by 1.\n            res++;\n    }\n    \n    //use level order traversal and calling the function help for each node\n    int averageOfSubtree(TreeNode* root) \n    {\n        int res=0;\n        queue<TreeNode*> q;\n        q.push(root);\n        while(!q.empty())\n        {\n            int n = q.size();\n            for(int i=0; i<n; i++)\n            {\n                TreeNode* node = q.front();\n                q.pop();\n                help(node, res);\n                if(node->left) q.push(node->left);\n                if(node->right) q.push(node->right);\n            }\n        }\n        return res;       \n    }\n};\n```\n\n**Approach-2: Use DFS**\n```\nclass Solution {\npublic:\n    pair<int, int> dfs(TreeNode* node, int &cnt) \n    {\n        if (!node)\n            return {0, 0};\n\n        auto left_subtree = dfs(node->left, cnt);\n        auto right_subtree = dfs(node->right, cnt);\n\n        int sum = left_subtree.first + right_subtree.first + node->val;\n        int count = 1 + left_subtree.second + right_subtree.second;\n\n        if(sum/count == node->val)\n            cnt++; //if average equals to node value, increase the count\n\n        return {sum, count};\n    }\n\n    int averageOfSubtree(TreeNode* root) \n    {\n        int cnt=0;\n        dfs(root, cnt);\n        return cnt;\n    }     \n};\n```
['Breadth-First Search', 'C', 'C++']