当前位置:网站首页>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;
}
边栏推荐
- 快解析结合友加畅捷U+
- F. Jinyu and its outer matrix (construction)
- Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
- Android Sqlite3基本命令
- B.构造一个简单的数列(贪心)
- token 过期后,如何自动续期?
- eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
- 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
- 数据库恢复
- 爬虫——动作链、xpath、打码平台使用
猜你喜欢
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
X射线掠入射聚焦反射镜
leetcode:250. 统计同值子树
leetcode:259. 较小的三数之和
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
属于程序猿的浪漫
快解析结合千方百剂
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
Go 语言快速入门指南: 变量和常量
随机推荐
G.登山小分队(暴力&dfs)
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
How to write SQL statements: the usage of Update, Case, and Select together
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
[LeetCode] 38. Appearance sequence
JCMsuite应用:倾斜平面波传播透过光阑的传输
如何确定异步 I/O 瓶颈
G. Mountaineering Squad (violence & dfs)
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
一看就会的Chromedriver(谷歌浏览器驱动)安装教程
Makefile 语法及使用笔记
第十六章 源代码文件 REST API 教程(一)
没有Project Facets的解决方法
数据库恢复
Crawler - action chain, xpath, coding platform use
1401 - Web technology 】 【 introduction to graphical Canvas
B.构造一个简单的数列(贪心)
基于 Next.js实现在线Excel
ASA归因:如何评估关键词的投放价值