Count Nodes Equal to Average of Subtree LeetCode Solution

Problem – Count Nodes Equal to Average of Subtree leetCode Solution

Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

Note:

  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • subtree of root is a tree consisting of root and all of its descendants.

Example 1:

Input: root = [4,8,5,0,1,null,6]
Output: 5
Explanation: 
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
For the node with value 0: The average of its subtree is 0 / 1 = 0.
For the node with value 1: The average of its subtree is 1 / 1 = 1.
For the node with value 6: The average of its subtree is 6 / 1 = 6.

Example 2:

Input: root = [1]
Output: 1
Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • 0 <= Node.val <= 1000

Count Nodes Equal to Average of Subtree LeetCode Solution in C++

array<int, 3> dfs(TreeNode* n) {
    if (n == nullptr)
        return {0, 0, 0};
    auto p1 = dfs(n->left), p2 = dfs(n->right);
    int sum = p1[0] + p2[0] + n->val, count = 1 + p1[1] + p2[1];
    return {sum, count, p1[2] + p2[2] + (n->val == sum / count)};
}
int averageOfSubtree(TreeNode* root) {
    return dfs(root)[2];
}

Count Nodes Equal to Average of Subtree LeetCode Solution in Java

class Solution {
    int res = 0;
    public int averageOfSubtree(TreeNode root) {
        dfs(root);
        return res;
    }
    
    private int[] dfs(TreeNode node) {
        if(node == null) {
            return new int[] {0,0};
        }
        
        int[] left = dfs(node.left);
        int[] right = dfs(node.right);
        
        int currSum = left[0] + right[0] + node.val;
        int currCount = left[1] + right[1] + 1;
        
        if(currSum / currCount == node.val) {
            res++;
        }
            
        return new int[] {currSum, currCount};
    }
}

Count Nodes Equal to Average of Subtree LeetCode Solution in python

class Solution:
    def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
        
        
        def calculate_average(root):
            if root:
                self.summ+=root.val
                self.nodecount+=1
                calculate_average(root.left)
                calculate_average(root.right)
        
        
        def calculate_for_each_node(root):
            if root:
                self.summ = 0
                self.nodecount = 0
                calculate_average(root)
                if ((self.summ)//(self.nodecount)) == root.val:
                    self.count+=1 
                calculate_for_each_node(root.left)
                calculate_for_each_node(root.right)
                
                
        self.count = 0
        calculate_for_each_node(root)       
        return self.count

Count Nodes Equal to Average of Subtree LeetCode Solution Review:

In our experience, we suggest you solve this Count Nodes Equal to Average of Subtree and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Count Nodes Equal to Average of Subtree LeetCode Solution

Conclusion:

I hope this Count Nodes Equal to Average of Subtree LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.