当前位置:网站首页>2265. 统计值等于子树平均值的节点数
2265. 统计值等于子树平均值的节点数
2022-06-25 06:40:00 【Mr Gao】
2265. 统计值等于子树平均值的节点数\
给你一棵二叉树的根节点 root ,找出并返回满足要求的节点数,要求节点的值等于其 子树 中值的 平均值 。
注意:
n 个元素的平均值可以由 n 个元素 求和 然后再除以 n ,并 向下舍入 到最近的整数。
root 的 子树 由 root 和它的所有后代组成。
示例 1:
输入:root = [4,8,5,0,1,null,6]
输出:5
解释:
对值为 4 的节点:子树的平均值 (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4 。
对值为 5 的节点:子树的平均值 (5 + 6) / 2 = 11 / 2 = 5 。
对值为 0 的节点:子树的平均值 0 / 1 = 0 。
对值为 1 的节点:子树的平均值 1 / 1 = 1 。
对值为 6 的节点:子树的平均值 6 / 1 = 6 。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int num;
int* dfs(struct TreeNode* root,int *data){
if(root){
int *a=dfs(root->left,data);
int numa=a[0];
int vala=a[1];
int *b=dfs(root->right,data);
int numb=b[0];
int valb=b[1];
int sum=vala+valb+root->val;
if(sum/(numa+numb+1)==root->val){
num++;
}
data[0]=numa+numb+1;
data[1]=sum;
return data;
}
else{
data[0]=0;
data[1]=0;
return data;
}
}
int averageOfSubtree(struct TreeNode* root){
num=0;
int *data=(int*)malloc(sizeof(int)*2);
int *a=dfs(root,data);
return num;
}
边栏推荐
- 【Qt】快捷键
- [batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
- Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
- VectorDraw Web Library 10.10
- 三年营收连续下滑,天地壹号困在醋饮料里
- Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
- 图扑软件数字孪生 3D 风电场,智慧风电之海上风电
- 指南针可以开股票账户吗?安全吗?
- Full range of isolator chips with integrated isolated power supply
- Cglib dynamic proxy
猜你喜欢

Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field

【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)

微信小程序开通客服消息功能开发

MySQL facet 01

Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer

Application of point cloud intelligent drawing in intelligent construction site

【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)

OAuth 2.0一键登录那些事

Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
![对链表进行插入排序[dummy统一操作+断链核心--被动节点]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
随机推荐
The method of judging whether triode can amplify AC signal
Static bit rate (CBR) and dynamic bit rate (VBR)
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
差点被这波Handler 面试连环炮带走~
Cglib dynamic proxy
Application of point cloud intelligent drawing in intelligent construction site
Sichuan earth microelectronics 8-channel isolated digital input receiver
Can I open a stock account with a compass? Is it safe?
Mysql database import SQL file display garbled code
smartBugs安装小问题总结
VectorDraw Developer Framework 10.10
useMemo模拟useCallback
Domestic MCU perfectly replaces STM chip model of Italy France
Runtime——methods成员变量,cache成员变量
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
Vscode official configuration synchronization scheme
Access to foreign lead domain name mailbox
Tempest HDMI leak receive 1
Redis learning notes
JDBC-DAO层实现