当前位置:网站首页>2265. number of nodes with statistical value equal to the average value of subtree
2265. number of nodes with statistical value equal to the average value of subtree
2022-06-25 07:52:00 【Mr Gao】
2265. The number of nodes whose statistical value is equal to the average value of the subtree \
Give you the root node of a binary tree root , Find and return the number of nodes that meet the requirements , The value of the node is required to be equal to its subtree median Average .
Be careful :
n The average value of elements can be determined by n Elements Sum up Then divide by n , and Round down To the nearest integer .
root Of subtree from root And all its descendants .
Example 1:
Input :root = [4,8,5,0,1,null,6]
Output :5
explain :
The right value is 4 The node of : Average value of subtree (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4 .
The right value is 5 The node of : Average value of subtree (5 + 6) / 2 = 11 / 2 = 5 .
The right value is 0 The node of : Average value of subtree 0 / 1 = 0 .
The right value is 1 The node of : Average value of subtree 1 / 1 = 1 .
The right value is 6 The node of : Average value of subtree 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;
}
边栏推荐
- 基于STM32MP157调试MIPI-DSI屏幕
- NSIS silent installation vs2013 runtime
- [Video] ffplay uses MJPEG format to play USB camera
- 27. 移除元素
- Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134 vulnerability analysis and protection
- [little knowledge] PCB proofing process
- 【QT】qtcreator便捷快捷键以及QML介绍
- Home environment monitoring system design (PC version) (mobile app version to be determined)
- The method of judging whether triode can amplify AC signal
- [distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
猜你喜欢

CAN总线工作状况和信号质量“体检”

基于地面点稀少的LiDAR点云的茂密森林蓄积量估算

差点被这波Handler 面试连环炮带走~

【深度学习 轻量型backbone】2022 EdgeViTs CVPR

一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局

test

神经网络与深度学习-3- 机器学习简单示例-PyTorch

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

传统的IO存在什么问题?为什么引入零拷贝的?

Estimation of dense forest volume based on LIDAR point cloud with few ground points
随机推荐
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
C reads XML on the web
WinForm实现窗口始终在顶层
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
微信小程序入门记录
Basic use of ActiveMQ in Message Oriented Middleware
OAuth 2.0 one click login
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
Take you through the normalization flow of GaN
El input to add words to the tail
Buckle 78: subset
个人域名和企业域名的区别
C#中如何调整图像大小
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
Access to foreign lead domain name mailbox
What are the problems with traditional IO? Why is zero copy introduced?
【QT】qtcreator便捷快捷键以及QML介绍
基于激光雷达的林业调查常用术语及含义锦集
How much do you know about electronic components on PCB?
双三次差值bicubic