当前位置:网站首页>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;
}
边栏推荐
- Set partition minimum difference problem (01 knapsack)
- Android Sqlite3基本命令
- NPDP|作为产品经理,如何快速提升自身业务素养?
- The Internet of things application development trend
- 基于 Next.js实现在线Excel
- Crawler - action chain, xpath, coding platform use
- 谷歌插件.crx文件下载后被自动删除的解决方法
- 蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
- leetcode:255 验证前序遍历序列二叉搜索树
- AOSP built-in APP franchise rights white list
猜你喜欢
随机推荐
7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
编译型与解释型编程语言的区别
兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
广告电商系统开发功能只订单处理
word2003按空格键为什么会出现小数点
MySQL【窗口函数】【共用表表达式】
1403. 非递增顺序的最小子序列
Leetcode: 215 disorderly to find the first big k element in the array
F. Jinyu and its outer matrix (construction)
基本介绍PLSQL
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
leetcode:253. 至少需要多少间会议室
数据链路层-------以太网协议
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
Oracle 数据库用户创建、重启、导入导出
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
Why does the decimal point appear when I press the space bar in word 2003?
X射线掠入射聚焦反射镜
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
化繁为简,聊一聊复制状态机系统架构抽象