当前位置:网站首页>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;
}
边栏推荐
- Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
- Evolution of Alibaba e-commerce architecture
- 【Qt】快捷键
- PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
- C get the version number of exe - file version and assembly version
- [batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
- Audio (V) audio feature extraction
- Vscode official configuration synchronization scheme
- Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
- Construction of occupancy grid map
猜你喜欢

Vscode official configuration synchronization scheme

Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute

NPM install reports an error: gyp err! configure error

Genuine photoshop2022 purchase experience sharing

Application of point cloud intelligent drawing in intelligent construction site
![[leetcode] two num · sum of two numbers](/img/9e/79c9deae9f853a265b5b536998b655.png)
[leetcode] two num · sum of two numbers

Path planner based on time potential function in dynamic environment

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

Introduction to Sichuan Tuwei ca-is3082wx isolated rs-485/rs-422 transceiver

Tempest HDMI leak receive 1
随机推荐
RTKLIB-b33版本中GALILEO广播星历存储问题
音频(五)音频特征提取
shell小技巧(一百三十四)简单的键盘输入记录器
函数模板_类模板
Runtime——methods成员变量,cache成员变量
realsense d455 semantic_ Slam implements semantic octree mapping
Bicubic difference
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
[batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
VectorDraw Web Library 10.10
[QT] shortcut key
Cglib dynamic proxy
國外LEAD域名郵箱獲取途徑
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
Manufacturing process of PCB 2021-10-11
realsense d455 semantic_slam实现语义八叉树建图
Genuine photoshop2022 purchase experience sharing