easy-soln | well-explained
Please upvote if you found helpful\uD83D\uDE0A\n#ReviseWithArsh #6Companies30Days Challenge 2023\nChellenge Company 3: Adobe\nQ9. Count  nodes equal to average 
# Please upvote if you found helpful\uD83D\uDE0A\n***#ReviseWithArsh #6Companies30Days Challenge 2023\nChellenge Company 3: Adobe\nQ9. Count  nodes equal to average of Subtree***\n\n# Complexity\n- Time complexity:O(n)\n- Space complexity: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 {\n    int dfs(TreeNode* root, int &sum, int &count){\n    // Base case: if root is null, return 0 for both sum and count\n    if(root==NULL){\n            sum=0,count=0;\n            return 0;\n    }\n    // Initialize variables to store sum and count of left and right subtrees\n    int sum1 = 0, count1 = 0, sum2 = 0, count2 = 0;\n    int ans1 = dfs(root->left, sum1, count1); // Recursively calculate sum and count of left subtree\n    int ans2 = dfs(root->right, sum2, count2); // Recursively calculate sum and count of right subtree\n\n    int ans = ans1+ans2; // Initialize ans as the total number of subtrees with equal average\n    sum = sum1+sum2+root->val; // Calculate sum of current subtree\n    count = count1+count2+1; // Calculate count of current subtree\n    int avg = (sum/(double)count); // Calculate average of current subtree\n\n    // Check if current root\'s value is equal to the average of the subtree\n    if(avg == root->val)\n        ans++;\n\n    return ans;\n}\n\n    public:\n        int averageOfSubtree(TreeNode* root) {\n        int sum, count;\n        return dfs(root, sum, count);\n        }\n}; \n```
['C++']