Simple java Solution (Post Order Traversal) beats 100% in time.
Intuition\nThis is a problem in which we need to find the average at every node. when you think about it what are the things which we need to compute the averag
# Intuition\nThis is a problem in which we need to find the average at every node. when you think about it what are the things which we need to compute the average-> the sum and the total number of elements involved.\nso you have to solve two sub problems which are to find the sum at each node and the number nodes in the given subtree.\n\n# Approach\nwe follow the approch of dividing the bigger problems into smaller chuncks. As we have discussed in the intution we need to solve both the problems. \nwhy dont we solve the two problems at once and also carry forward the results we got so far while doing this.\nSo we consider the left subtree and the right subtree and then consolidate the results which you got from both of them and create a result for the current subtree. this process goes on and on and on... until we reach the main root. then we go back to the main function and return the final answer.\n\n# Complexity\n- Time complexity:\n$$O(n)$$ as we visit each node once. where n is the number of nodes.\n\n- Space complexity:\n$$O(h)$$ this is due to the call stack space. where h is the height of the tree. \n\n# Code\n```\nclass Solution {\n    //return array is going to be of size 3\n    public int[] helper(TreeNode root){\n        if(root==null){\n            return new int[3];\n        }\n        int[] leftRes = helper(root.left);\n        int[] rightRes = helper(root.right);\n\n        int totalSum = leftRes[0] + rightRes[0] + root.val;\n        int totalCount = leftRes[1] + rightRes[1] + 1;\n        int currentAverage = totalSum / totalCount;\n        int currentResult = currentAverage == root.val?leftRes[2] + rightRes[2] + 1 : leftRes[2] + rightRes[2];\n\n        return new int[]{totalSum, totalCount, currentResult};\n    }\n    public int averageOfSubtree(TreeNode root) {\n        int[] res = helper(root);\n        return res[2];\n    }\n}\n```\n\nplease let me know if there are any room for improvement or if there is any error with the complexities.\nThank you for reading. Happy coding :)
['Java']