Easiest Java Code with Neat Explaination , Beats 100%.
Intuition\n Describe your first thoughts on how to solve this problem. \n     DFS\n\n# Approach\n Describe your approach to solving the problem. \n     DFS\nThi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n     DFS\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n     DFS\n*This problem is pretty simple , it wants you to calculate the sum of each subtrees and check if their sums Average is equal to root Node.\nWe can easily calculate the sum of subtrees using DFS (Recursion) , but the important thing is to find the number of nodes , so instead of calculating both of them separately i introduced a class called    " Pair " , this class stores the data in each subtrees and the number of nodes in each subtree. By having all these information i think it\'s damn easy to calculate the Nodes Equal to Average of Subtree.\nI Hope this helps , an upvote would mean so much to me :)*\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n\n# Code\n```\nclass Solution {\n    static int count;\n    static class Pair{\n        int root_val;\n        int cnt;\n        Pair(int root_val,int cnt){\n            this.root_val=root_val;\n            this.cnt=cnt;\n        }\n    }\n    public static Pair calc(TreeNode root){\n        if(root==null) return new Pair(0,0);\n        Pair left=calc(root.left);\n        Pair right=calc(root.right);\n        int tot_sum=left.root_val+right.root_val+root.val;\n        int tot_nodes=left.cnt+right.cnt+1;\n        if(tot_sum/tot_nodes==root.val) count++;\n        return new Pair(left.root_val+right.root_val+root.val,left.cnt+right.cnt+1);\n    }\n    public int averageOfSubtree(TreeNode root) {\n        count=0;\n        calc(root);\n        int temp=count;\n        count=0;\n        return temp;\n    }\n}\n```
['Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'Java']