Simple & Algorithmic Solution :
Algorithm : \n> ###  Calculate the sum and count of the left subtree and also right subtree and store them in a Pair object.\n\n> ### Calculate the avg using th
# Algorithm : \n> ###  *Calculate the sum and count of the left subtree and also right subtree and store them in a Pair object.*\n\n> ### *Calculate the avg using the sum\'s and count\'s from left and right subtrees and check if the avg equals to the root\'s data if equal then increase the ans, Now you can return a new Pair object with the left subtree\'s sum and right subtree\'s and add root\'s and also we need to have total count.* \n\n# Code\n```java  []\nclass Pair\n{\n    int count = 0,sum = 0;\n    Pair(int count,int sum)\n    {\n        this.count = count;\n        this.sum = sum;\n    }\n}\nclass Solution {\n    private int ans = 0;\n    private Pair function(TreeNode root)\n    {\n        if(root == null)\n            return new Pair(0,0);\n        Pair left = function(root.left);\n        Pair right = function(root.right);\n        int totalSum = left.sum+right.sum+root.val;\n        int totalCount = left.count+right.count+1;\n        int avg = (int)Math.round(totalSum/totalCount);\n        if(avg == root.val)\n            ans+=1;\n        return new Pair(totalCount,totalSum);   \n    }\n    public int averageOfSubtree(TreeNode root) {\n        function(root);\n        return ans;\n    }\n}\n```
['Tree', 'Depth-First Search', 'Binary Tree', 'C++', 'Java', 'Python3']