Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 625 Bytes

2265. Count Nodes Equal to Average of Subtree.md

File metadata and controls

25 lines (21 loc) · 625 Bytes

Code for ' 2265. Count Nodes Equal to Average of Subtree ' ( 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);
  }
}