Beats 100.00%of users with Java || Simple Java Solution || DFS
Approach\n Describe your approach to solving the problem. \nSimply visit each the nodes from bottom to top, get a helper funstion to save and return the number 
# Approach\n<!-- Describe your approach to solving the problem. -->\nSimply visit each the nodes from bottom to top, get a helper funstion to save and return the number of nodes and sum of nodes of each subtree, then compare the average with current node val to count the result.\nCan use an int array or Pair as the return type of the helper function.\n\n# Complexity\n- Time complexity: $$O(n)$$ as we visit each node exactly once.\n\n- Space complexity: $$O(n)$$ as $$O(1)$$ space is needed for each node.\n\n# Code\n```\nclass Solution {\n    int res = 0;\n\n    public int averageOfSubtree(TreeNode root) {\n        traverse(root);\n        return res;\n    }\n\n    private int[] traverse(TreeNode root) {\n        if (root == null) {\n            return null;\n        }\n        int[] left = traverse(root.left);\n        int[] right = traverse(root.right);\n\n        int count = 1;\n        int sum = root.val;\n        if (left != null) {\n            count += left[0];\n            sum += left[1];\n        }\n        if (right != null) {\n            count += right[0];\n            sum += right[1];\n        }\n        res += root.val == (sum / count) ? 1 : 0;\n        return new int[] {count, sum};\n    }\n}\n```
['Java']