当前位置:网站首页>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;
}
边栏推荐
- Problem solving-->Online OJ (18)
- 【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
- How to automatically renew the token after it expires?
- CF1527D MEX Tree (mex & tree & inclusive)
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
- Basic Introduction for PLSQL
- 基于数据库实现分布式锁
- 基于 Next.js实现在线Excel
- 小 P 周刊 Vol.13
- 【剑指offer33】二叉搜索树的后序遍历序列
猜你喜欢

数据库恢复

基于数据库实现分布式锁

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

1403. Minimum Subsequence in Non-Increasing Order

leetcode:250. 统计同值子树

leetcode:253. 至少需要多少间会议室

化繁为简,聊一聊复制状态机系统架构抽象

Basic Introduction for PLSQL

CloudCompare&PCL 点云按网格划分(点云分幅)

【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
随机推荐
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
Workaround without Project Facets
【剑指offer59】队列的最大值
LeetCode_模拟_中等_498.对角线遍历
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
1403. 非递增顺序的最小子序列
B. Construct a simple sequence (greedy)
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
Android Sqlite3 basic commands
Set partition minimum difference problem (01 knapsack)
世间几乎所有已知蛋白质结构,都被DeepMind开源了
How to automatically renew the token after it expires?
leetcode:250. 统计同值子树
Hangzhou Electric School Competition (Counter Attack Index)
How to fall in love with a programmer
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
谷歌插件.crx文件下载后被自动删除的解决方法
[Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
AOSP内置APP特许权限白名单