Fast recursion solution with notes by Java & Javascript ✅
java\n\n\nclass Solution {\n    \n    int res = 0;\n    \n    public int averageOfSubtree(TreeNode root) {\n        node t = new node(0,0);\n        counting(ro
**java**\n![image](https://assets.leetcode.com/users/images/3fb5d04d-9c49-4c61-8518-151fd1a13403_1668347151.616631.png)\n```\nclass Solution {\n    \n    int res = 0;\n    \n    public int averageOfSubtree(TreeNode root) {\n        node t = new node(0,0);\n        counting(root);\n        return res;\n    }\n    \n    // recursion method : \n    private node counting(TreeNode root){\n        \n        // base case :\n        if(root.left == null && root.right == null){\n            node temp = new node(root.val,1);\n            res++;\n            return temp;\n        }\n        node tempLeft = new node(0,0);\n        node tempRight = new node(0,0);\n        \n        // first check left :\n        if(root.left != null){\n            tempLeft = counting(root.left);\n        }\n        \n        // check right :\n        if(root.right != null){\n            tempRight = counting(root.right);\n        }\n        \n        // sum = values of nodes :\n        int sum = tempLeft.sum + tempRight.sum + root.val;\n        // count = number of nodes :\n        int count = tempLeft.count + tempRight.count + 1;\n        \n        if(sum/count == root.val) res++;\n        \n        // return the node :\n        node temp = new node(sum,count);\n        return temp;\n    }\n        \n}\n\nclass node{\n    int sum = 0;\n    int count = 0;\n    \n    node(int s, int c){\n        sum = s;\n        count = c;\n    }\n}\n```\n\n**Javascript**\n![image](https://assets.leetcode.com/users/images/b71dd70a-e898-4cc2-9ab3-849948723982_1668347194.1284957.png)\n```\nvar averageOfSubtree = function(root) {\n    let res = 0;\n    \n    // recursion method : \n    var counting = function(node){\n        \n        // base case :\n        if(!node.left && !node.right){\n            let tempArr = [node.val,1];\n            res++;\n            return tempArr;\n        }\n        \n        let tempLeft = [0,0];\n        let tempRight = [0,0];\n        \n        // check left :\n        if(node.left){\n            tempLeft = counting(node.left);\n        }\n        \n        // check right :\n        if(node.right){\n            tempRight = counting(node.right);\n        }\n        \n        // sum = values of nodes :\n        let sum = tempLeft[0] + tempRight[0] + node.val;\n        // count = number of nodes :\n        let count = tempLeft[1] + tempRight[1] + 1;\n        \n        if(Math.floor(sum/count) == node.val) res++;\n        \n        // return array :\n        let temp = [sum,count];\n        return temp;\n    }\n    \n    \n    counting(root);\n    return res;\n};\n```
['Recursion', 'Java', 'JavaScript']