Clean Code | beat 100% O(n) Solution
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co
# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n\n    public int averageOfSubtree(TreeNode root) {\n        return helper(root)[2];\n    }\n\n    public int[] helper(TreeNode root) {\n        if(root == null) return new int[] {0, 0, 0};\n\n        int[] left = helper(root.left);\n        int[]  right = helper(root.right);\n        \n        int sum = left[0] + right[0] + root.val;\n        int count = left[1] + right[1] + 1;\n        \n        return new int[] {sum, count, left[2] + right[2]  + (sum/count == root.val ? 1 : 0)};\n    }\n}\n```
['Python', 'C++', 'Java', 'Python3']