当前位置:网站首页>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;
}
边栏推荐
- MySQL【触发器】
- AOSP built-in APP franchise rights white list
- Basic Introduction for PLSQL
- How to Identify Asynchronous I/O Bottlenecks
- ICML 2022 | 图神经网络的局部增强
- 代码随想录笔记_动态规划_1049最后一块石头的重量II
- [LeetCode] 38. Appearance sequence
- Phasecraft连下两城,助力英国量子技术商业化加速!
- Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
- 输入输出流总结
猜你喜欢

Almost all known protein structures in the world are open sourced by DeepMind

一看就会的Chromedriver(谷歌浏览器驱动)安装教程

Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing

技术分享| 融合调度系统中的电子围栏功能说明

FRED Application: Capillary Electrophoresis System

MySQL【窗口函数】【共用表表达式】

Database recovery

期货开户之前要谈好最低手续费和交返

Win10无法访问移动硬盘怎么解决

1403. 非递增顺序的最小子序列
随机推荐
CF1527D MEX Tree(mex&树&容斥)
化繁为简,聊一聊复制状态机系统架构抽象
leetcode: 254. Combinations of factors
16. Learn MySQL Regular Expressions
xampp安装包含的组件有(php,perl,apche,mysql)
Hangzhou electric the competition team arrangement (ACM)
G. Mountaineering Squad (violence & dfs)
编译型与解释型编程语言的区别
兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
Oracle 数据库用户创建、重启、导入导出
Basic Introduction for PLSQL
ACL 2022 | 社会科学理论驱动的言论建模
leetcode:250. 统计同值子树
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
Rust 从入门到精通04-变量
xpath获取带命名空间节点注意事项
【硬件架构的艺术】学习笔记(1)亚稳态的世界
How to fall in love with a programmer
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来