首页 > 编程语言 >LeetCode 2265. Count Nodes Equal to Average of Subtree

LeetCode 2265. Count Nodes Equal to Average of Subtree

时间:2024-03-21 11:36:15浏览次数:24  
标签:Count TreeNode val int Average Equal subtree root average

原题链接在这里:https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/description/

题目:

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

题解:

DFS return sum of values in substree and count of nodes in subtree.

when sum/count == root.val, incremental the result by 1.

Time Complexity: O(n). n is the number of nodes in the tree.

Space: O(h). h is the height of the tree.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     int res = 0;
18     public int averageOfSubtree(TreeNode root) {
19         dfs(root);
20         return res;
21     }
22 
23     private int[] dfs(TreeNode root){
24         if(root == null){
25             return new int[]{0, 0};
26         }
27 
28         int[] l = dfs(root.left);
29         int[] r = dfs(root.right);
30         int sum = l[0] + r[0] + root.val;
31         int count = l[1] + r[1] + 1;
32         if(sum / count == root.val){
33             res++;
34         }
35 
36         return new int[]{sum, count};
37     }
38 }

类似Maximum Average Subtree.

标签:Count,TreeNode,val,int,Average,Equal,subtree,root,average
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18086980

相关文章

  • Counts the number of the messages received and sent
    我的博客园:https://www.cnblogs.com/CQman/本文版权归CQman和博客园共有,欢迎转载,但必须保留此段声明,并给出原文链接,谢谢合作。Symptom Countsthenumberofmessagesreceivedandsent统计接收和发送邮件的数量说明:管理员想知道:所有用户邮箱接受和发送的邮件数量注意:此......
  • SQL中的COUNT函数:深入理解COUNT(*)、COUNT(1)和COUNT(字段)的异同与应用
    SQL中的COUNT函数是一个非常强大的聚合函数,它可以用来统计表中满足特定条件的行数。COUNT函数有三种不同的用法:COUNT(*)、COUNT(1)和COUNT(字段),每种用法都有其特定的用途和性能考虑。COUNT(*)COUNT(*)用于统计表中的所有行,不论这些行的值是否为NULL。当你想要得到表中总行数时,......
  • CF999D Equalize the Remainders 题解
    题意给定一个长度为\(n\)的序列和一个模数\(m\),记\(c_i\)表示\(\bmodm\)后的结果为\(i\)的数的个数。现在可以使每个数增加\(1\),请问最少要操作多少次才能使所有\(c_i=\frac{n}{m}\)。并输出最后的序列。First.如何最小化操作次数由于每次操作会使\(c_{a_i\bm......
  • CodeForces 1943D2 Counting Is Fun (Hard Version)
    洛谷传送门CF传送门被自己的赛时智障操作气笑了。谁告诉你容斥钦定了几个要记到状态里面的。。。/tuu显然先找“好数组”的充要条件。对原数组\(a\)差分,设\(b_i=a_i-a_{i-1}\)。那么一次可以选择一对\((i,j)\)满足\(i\lej-2\),然后给\(b_i\)减\(1\),给\(b_......
  • 求字符串中某个字符的数量--long k = s.chars().filter(ch -> ch == c).count();
    classSolution{publiclongcountSubstrings(Strings,charc){longk=s.chars().filter(ch->ch==c).count(); //Java中的count()方法返回值是基本数据类型longreturnk*(k+1)/2;}}作者:灵茶山艾府链接:https://leetcode.cn/pr......
  • 论文解读(CGC)《Generating Counterfactual Hard Negative Samples for Graph Contrasti
    Note:[wechat:Y466551|可加勿骚扰,付费咨询]论文信息论文标题:GeneratingCounterfactualHardNegativeSamplesforGraphContrastiveLearning论文作者:论文来源:2023WWW论文地址:download 论文代码:download视屏讲解:click0-摘要图对比学习已经成为一种强大的无监督图......
  • P2633 Count on a tree 题解
    题目链接:Countonatree大概可以认为是树上主席树的板子我在之前的某些题解提到了,主席树一般来说有两个基本功能:可持久化功能,可以选择回退或者新增版本。对于可差性问题,可以有更好的转化为前缀和做法,常见的问题为权值类型问题。在树上的路径第\(k\)大,显然如果我们能......
  • 定义类——定义银行账户类Account
    定义一个类Account表示银行账户,Account类的要求如下:1、private的成员变量id表示账户账号,private的成员变量balance表示账号余额2、两个构造器,一个初始化账号id,默认余额为0.0;另一个初始化账号id和余额,具体可参考Main类中的调用;3、公有方法save(doublemoney)表示存钱,实现向......
  • YC256B [ 20240312 CQYC省选模拟赛 T2 ] count
    题意对于一个长度为\(n\)的排列\(P\)。你需要求出所有满足条件的长度为\(k\)的数列\(A\)的个数。\(A\)单调不减且\(1\leA_i\len\)\(\min_{j=1}^{A_1}P_j=\min_{j-1}^{A_i}P_j\)求出对于\(P_1=x\)的所有排列的满足条件的\(A\)的个数。Sol......
  • 自动生成hashcode和equals方法
    右键一键生成importjava.util.Objects;publicclassStudent{privateStringname;privateintage;publicStudent(){}publicStudent(Stringname,intage){this.name=name;this.age=age;}publicStringgetName(){......