当前位置:网站首页>leetcode:250. 统计同值子树
leetcode:250. 统计同值子树
2022-08-04 14:31:00 【OceanStar的学习笔记】
题目来源
题目描述

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {
}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
}
};
class Solution{
int countUnivalSubtrees(TreeNode* root){
}
};
题目解析

思路
- 首先,叶子节点都是同值子树
- 然后,对于非叶子节点:
- 如果仅仅存在左子树,那么必须左子树是同值子树&&左子树的节点值必须等于当前节点的值
- 如果仅仅存在右子树,那么必须右子树是同值子树&&右子树的节点值必须等于当前节点的值
- 如果存在左右子树,那么必须左右子树都是同值子树&&左子树的节点值必须等于当前节点的值&&右子树的节点值必须等于当前节点的值
实现
class Solution{
struct Info{
bool isUnival;
int cnt;
int val;
Info(bool isUnival, int cnt, int val) : isUnival(isUnival), cnt(cnt), val(val){
}
};
Info *process(TreeNode* root){
if(root == nullptr){
return nullptr;
}
auto left = process(root->left);
auto right = process(root->right);
int cnt = 0;
bool isUnival = false;
if(left == nullptr && right == nullptr){
isUnival = true;
cnt = 1;
}else if(left == nullptr){
isUnival = right->isUnival && right->val == root->val;
cnt = (isUnival? 1 : 0) + right->cnt;
}else if(right == nullptr){
isUnival = left->isUnival && left->val == root->val;
cnt = (isUnival? 1 : 0) + left->cnt;
}else{
isUnival = right->isUnival && right->val == root->val && left->isUnival && left->val == root->val;
cnt = (isUnival? 1 : 0) + left->cnt + right->cnt;
}
return new Info(isUnival, cnt, root->val);
}
public:
int countUnivalSubtrees(TreeNode* root){
if(root == nullptr){
return 0;
}
return process(root)->cnt;
}
};
测试
int main() {
TreeNode* root = new TreeNode(5);
root->left = new TreeNode(1);
root->right = new TreeNode(5);
root->left->left = new TreeNode(5);
root->left->right = new TreeNode(5);
root->right->right = new TreeNode(5);
Solution a;
std::cout << a.countUnivalSubtrees(root) << "\n";
return 1;
}
边栏推荐
- [in-depth study of 4 g / 5 g / 6 g project - 50] : URLLC - 16 - the 3 GPP URLLC agreement, specification, technical principle of depth interpretation - 10 - high reliability technology - 1 - low codin
- vim 常用操作命令
- G. Mountaineering Squad (violence & dfs)
- metaRTC5.0新版本支持mbedtls(PolarSSL)
- centos7安装mysql急速版
- zabbix自定义图形
- 南瓜科学产品升级 开启益智探索新篇章
- 如何和程序员谈恋爱
- Crawler - action chain, xpath, coding platform use
- License server system does not support this version of this feature
猜你喜欢

Fuse bit of AVR study notes

【剑指offer33】二叉搜索树的后序遍历序列

token 过期后,如何自动续期?

四平方和,激光炸弹

Redis 复习计划 - Redis主从数据一致性和哨兵机制

Problem solving-->Online OJ (18)

Google plug-in. Download contents file is automatically deleted after solution

Database recovery

自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展

Almost all known protein structures in the world are open sourced by DeepMind
随机推荐
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
Convolutional Neural Network Basics
word2003按空格键为什么会出现小数点
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
第十六章 源代码文件 REST API 教程(一)
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
vim 常用操作命令
Phasecraft连下两城,助力英国量子技术商业化加速!
oracle+RAC+linux5.1所需要安装的包
物联网应用发展趋势
ICML 2022 | 图神经网络的局部增强
JCMsuite应用:倾斜平面波传播透过光阑的传输
Almost all known protein structures in the world are open sourced by DeepMind
南瓜科学产品升级 开启益智探索新篇章
How to Identify Asynchronous I/O Bottlenecks
Find My技术|防止你的宠物跑丢,苹果Find My技术可以帮到你
centos7安装mysql急速版
并发程序的隐藏杀手——假共享(False Sharing)
CF1527D MEX Tree (mex & tree & inclusive)
阴影初始化【5】