当前位置:网站首页>leetcode: 250. Count subtrees of equal value
leetcode: 250. Count subtrees of equal value
2022-08-04 14:38:00 【OceanStar's study notes】
题目来源
题目描述

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){
}
};
题目解析

思路
- 首先,Leaf nodes are all subtrees of equal value
- 然后,对于非叶子节点:
- If only the left subtree exists,Then the left subtree must be the same value subtree&&The node value of the left subtree must be equal to the value of the current node
- If only the right subtree exists,Then the right subtree must be the same value subtree&&The node value of the right subtree must be equal to the value of the current node
- 如果存在左右子树,Then the left and right subtrees must be equal subtrees&&The node value of the left subtree must be equal to the value of the current node&&The node value of the right subtree must be equal to the value of the current node
实现
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;
}
边栏推荐
- 特殊品种的二次开户验资金额
- 分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
- 【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
- 郑轻新生校赛和中工选拔赛题解
- 爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
- 自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
- This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
- leetcode:215无序数组中找第k大的元素
- G.登山小分队(暴力&dfs)
- 小 P 周刊 Vol.13
猜你喜欢

世间几乎所有已知蛋白质结构,都被DeepMind开源了

leetcode:215无序数组中找第k大的元素

【模型部署与业务落地】基于量化芯片的损失分析

Theory 1: Deep Learning - Detailed Explanation of the LetNet Model

化繁为简,聊一聊复制状态机系统架构抽象
![[LeetCode] 38. Appearance sequence](/img/d6/092796b57844d5d30f3ed123a1b98a.png)
[LeetCode] 38. Appearance sequence

1403. 非递增顺序的最小子序列

How to automatically renew the token after it expires?
![[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution](/img/1c/3c8c323e6ee3406d202e07f85bab21.jpg)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution

爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
随机推荐
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况
爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
AOSP built-in APP franchise rights white list
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
Android Sqlite3基本命令
FRED Application: Capillary Electrophoresis System
Zheng Qing freshmen school competition and middle-aged engineering selection competition
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
技术分享| 小程序实现音视频通话
leetcode:241. 为运算表达式设计优先级
物联网应用发展趋势
我爱七夕哈哈哈
How to fall in love with a programmer
metaRTC5.0新版本支持mbedtls(PolarSSL)
vim common operation commands
AOSP内置APP特许权限白名单
G. Mountaineering Squad (violence & dfs)
编译型与解释型编程语言的区别