2265. Count Nodes Equal to Average of Subtree LeetCode Solution

In this guide, you will get 2265. Count Nodes Equal to Average of Subtree LeetCode Solution with the best time and space complexity. The solution to Count Nodes Equal to Average of Subtree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Nodes Equal to Average of Subtree solution in C++
  4. Count Nodes Equal to Average of Subtree solution in Java
  5. Count Nodes Equal to Average of Subtree solution in Python
  6. Additional Resources
2265. Count Nodes Equal to Average of Subtree LeetCode Solution image

Problem Statement of Count Nodes Equal to Average of Subtree

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.
A 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

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

class Solution {
 public:
  int averageOfSubtree(TreeNode* root) {
    int ans = 0;
    dfs(root, ans);
    return ans;
  }

 private:
  pair<int, int> dfs(TreeNode* root, int& ans) {
    if (root == nullptr)
      return {0, 0};
    const auto [leftSum, leftCount] = dfs(root->left, ans);
    const auto [rightSum, rightCount] = dfs(root->right, ans);
    const int sum = root->val + leftSum + rightSum;
    const int count = 1 + leftCount + rightCount;
    if (sum / count == root->val)
      ++ans;
    return {sum, count};
  }
};
/* code provided by PROGIEZ */

2265. Count Nodes Equal to Average of Subtree LeetCode Solution in Java

class Solution {
  public int averageOfSubtree(TreeNode root) {
    dfs(root);
    return ans;
  }

  private int ans = 0;

  private Pair<Integer, Integer> dfs(TreeNode root) {
    if (root == null)
      return new Pair<>(0, 0);
    Pair<Integer, Integer> left = dfs(root.left);
    Pair<Integer, Integer> right = dfs(root.right);
    final int sum = root.val + left.getKey() + right.getKey();
    final int count = 1 + left.getValue() + right.getValue();
    if (sum / count == root.val)
      ++ans;
    return new Pair<>(sum, count);
  }
}
// code provided by PROGIEZ

2265. Count Nodes Equal to Average of Subtree LeetCode Solution in Python

class Solution:
  def averageOfSubtree(self, root: TreeNode | None) -> int:
    ans = 0

    def dfs(root: TreeNode | None) -> tuple[int, int]:
      nonlocal ans
      if not root:
        return (0, 0)
      leftSum, leftCount = dfs(root.left)
      rightSum, rightCount = dfs(root.right)
      summ = root.val + leftSum + rightSum
      count = 1 + leftCount + rightCount
      if summ // count == root.val:
        ans += 1
      return (summ, count)

    dfs(root)
    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.