EASY C++ Solution
\n# Approach\nThe provided code is for a C++ class named Solution, which seems to be related to binary trees. The primary function in this class, averageOfSubtr
\n# Approach\nThe provided code is for a C++ class named `Solution`, which seems to be related to binary trees. The primary function in this class, `averageOfSubtree`, calculates the number of nodes in a binary tree where the value of the node is equal to the average value of its subtree. Here\'s an approach to understanding how this code works:\n\n1. The `count` function is a recursive helper function that takes a `TreeNode*` (representing the root of a subtree) and a reference to an integer `nodecount` (used to count the nodes meeting the condition).\n\n2. Inside the `count` function, the base case checks if the `root` is `NULL`, which represents an empty subtree. In this case, it returns a pair `{0, 0}`, indicating that the sum and total element count of the subtree are both zero.\n\n3. If the `root` is not `NULL`, it recursively calls the `count` function on the left and right subtrees (`lh` and `rh`), and it calculates the sum of values in the subtree (`sum`) and the total element count in the subtree (`totalele`).\n\n4. It checks if the average of the subtree (calculated as `sum / totalele`) is equal to the value of the current node (`root->val`). If it is, the `nodecount` is incremented.\n\n5. The `count` function returns a pair `{sum, totalele}` representing the sum and total element count for the current subtree.\n\n6. The `averageOfSubtree` function is the entry point. It initializes a variable `cnt` to zero and then calls the `count` function, passing the root of the entire tree and the `cnt` variable as a reference.\n\n7. Finally, the `averageOfSubtree` function returns the value of `cnt`, which contains the count of nodes that meet the condition of having an average value equal to the node\'s value.\n\nThis code recursively traverses the binary tree, counting the nodes that meet the specified condition and returns the count of such nodes.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:0(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npair<int,int> count(TreeNode* root, int& nodecount)\n{\n    if(root == NULL)\n    {\n        return {0,0};\n    }\n    auto lh = count(root->left,nodecount);\n    auto rh = count(root->right,nodecount);\n\n    int sum = lh.first + rh.first + root->val;\n    int totalele = lh.second + rh.second + 1;\n\n    if(sum/totalele == root->val)\n    {\n        nodecount++;\n    }\n    return {sum,totalele};\n}\npublic:\n    int averageOfSubtree(TreeNode* root) {\n        int cnt = 0;\n        count(root,cnt); \n        return cnt;\n    }\n};\n```
['Tree', 'Depth-First Search', 'Binary Tree', 'C++']