C++ || Easy Solution || Explanation With Comments || Using BFS
Please Upvote If It Helps\n\n\nclass Solution {\npublic:\n    // this function will calculate the sum for the current element subtree\n    void average(TreeNode
**Please Upvote If It Helps**\n\n```\nclass Solution {\npublic:\n    // this function will calculate the sum for the current element subtree\n    void average(TreeNode* node, int& ans)\n    {\n        int sum=0;                       // it will calculate the subtree sum for current node#\n        int cnt=0;                       // it will store the number of values that are present in subtree\n        \n        // pushing current node to the queue\n        queue<TreeNode*> q;\n        q.push(node);\n        \n        // BFS\n        while(!q.empty())\n        {\n            int s = q.size();\n            \n            // iterating over q size\n            for(int i=0;i<s;i++)\n            {\n                // extracting the queue\'s front element\n                TreeNode* nod = q.front();\n                q.pop();\n                \n                // after taking the elemets , add that val into the sum and incraese the value\'s cnt by 1\n                sum += nod->val;\n                cnt++;\n                \n                // check for current node left and right\n                if(nod->left)\n                    q.push(nod->left);\n                if(nod->right)\n                    q.push(nod->right);\n            }\n        }\n        if(sum/cnt==node->val)\n            ans++;\n    }\n    \n    // this function will make sure to iterate all the nodes\n    int averageOfSubtree(TreeNode* root) \n    {\n        // we have to traversal all the given tree and for each node we have to traverse it\'s own subtree\n        // so we can use both BFS & DFS here \n        // so we will solve this problem by using BFS \n        \n        int ans = 0;\n        queue<TreeNode*> q;\n        q.push(root);\n        \n        // BFS\n        while(!q.empty())\n        {\n            // extracting the size of the queue\n            int s = q.size();\n            \n            // now check for the elements that avaliable in queue\n            for(int i=0;i<s;i++)\n            {\n                // taking out the front element\n                TreeNode* node = q.front();\n                q.pop();\n                \n                // call the function for current node and calculate its avg\n                average(node,ans);\n                \n                // after counting the avg\'s chech there is left or right available \n                if(node->left)\n                    q.push(node->left);\n                if(node->right)\n                    q.push(node->right);\n            }\n        }\n        return ans;\n    }\n};\n```
['Tree', 'Breadth-First Search']