Java Clean DFS
java\nprivate int averageEqualsValueNodes = 0;\n\n    public int averageOfSubtree(TreeNode root) {\n        sumAndCount(root);\n        return averageEqualsValu
```java\nprivate int averageEqualsValueNodes = 0;\n\n    public int averageOfSubtree(TreeNode root) {\n        sumAndCount(root);\n        return averageEqualsValueNodes;\n    }\n\n\n    private SumAndCount sumAndCount(TreeNode node) {\n        if (node == null) return new SumAndCount(0, 0);\n\n        SumAndCount left = sumAndCount(node.left);\n        SumAndCount right = sumAndCount(node.right);\n\n        int sum = left.sum() + right.sum() + node.val;\n        int count = left.count() + right.count() + 1;\n\n        if (sum / count == node.val)\n            averageEqualsValueNodes++;\n\n        return new SumAndCount(sum, count);\n    }\n\n    private record SumAndCount(int sum, int count) {}\n```
['Java']