当前位置:网站首页>508. Most Frequent Subtree Sum
508. Most Frequent Subtree Sum
2022-06-21 19:39:00 【SUNNY_ CHANGQI】
The description of the problem
Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.
The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
source : Power button (LeetCode)
link :https://leetcode.cn/problems/most-frequent-subtree-sum
an example
Input: root = [5,2,-3]
Output: [2,-3,4]
The intuition for this problem
the recursive method to add all the substree summarization
use the unordered_map STL to find collect the summary value and its appearance time
The corresponding codes
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
// Definition for a binary tree node.
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 {
private:
unordered_map<int, int> cnt_;
int max_cnt_;
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> res;
tree_sum(root);
for (auto it = cnt_.begin(); it != cnt_.end(); ++it) {
if (it->second == max_cnt_) {
res.emplace_back(it->first);
}
}
return res;
}
int tree_sum(TreeNode* root) {
if(!root) return 0;
int sum = root->val + tree_sum(root->left) + tree_sum(root->right);
++cnt_[sum];
max_cnt_ = max(max_cnt_, cnt_[sum]);
return sum;
}
};
int main()
{
Solution s;
TreeNode* root = new TreeNode(5);
root->left = new TreeNode(2);
root->right = new TreeNode(-3);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
root->right->left = new TreeNode(-2);
root->right->right = new TreeNode(7);
auto res = s.findFrequentTreeSum(root);
cout << "res: ";
for (auto &i : res) {
std::cout << i << " ";
}
return 0;
}
The result
$ ./test
res: 13 2 7 6 3 -2 1 %

边栏推荐
- C# Mapster 对象映射器学习
- 【区间和专题の前缀和】线段树(动态开点)运用题
- API interface for discharge summary identification - medical bill OCR identification / discharge diagnosis record / electronic medical record / claim settlement service
- MFC interface library bcgcontrolbar v33.0 - Desktop alert window, grid control upgrade
- 《Go题库·9》同一个协程里面,对无缓冲channel同时发送和接收数据有什么问题
- 2022年6月25日PMP考试通关宝典-5
- Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南
- 技术分享 | MySQL:caching_sha2_password 快速问答
- 【面试高频题】难度 1.5/5,常见的二分双指针面试题
- 力扣每日一练之双指针1Day8
猜你喜欢

Notes on writing questions in C language -- find s=a+aa+aaa+aaaa+aa Value of a

如何在Chrome浏览器中临时修改SameSite=None和Secure

使用uniapp框架搭建浙里办微应用(单点登录、埋点、适老化、RPC网关)
![Dynamic programming [II] (linear DP)](/img/c9/f4fbc78d24320e3bd915d99de5837b.jpg)
Dynamic programming [II] (linear DP)

After Hongmeng, Huawei announced that it would donate to Euler again. What impact is expected to be brought to the industry by the donations of Hongmeng and Euler?

如何使用DevExpress WPF在WinUI中创建第一个MVVM应用?
![[Shangshui Shuo series] day one](/img/09/ab31cc494d726e896799d21fa02502.png)
[Shangshui Shuo series] day one

2022年下半年传统产品经理国际资格认证招生简章(NPDP)

鸿蒙版“抖音”,这体验感赞

在Qt中设置程序图标的方法介绍
随机推荐
vivo 容器集群监控系统架构与实践
[comprehensive pen test] difficulty 2.5/5: "tree array" and "double tree array optimization"
Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南
文件上传漏洞靶场分析 UPLOAD_LABS
文献分析 Citespace 6.1.2 下载及安装教程
OGG-21.3 报错 OGG-00768 Failed to Map database character to ULibCharSet
In air operation, only distance mapping is used to robustly locate occluded targets (ral2022)
品牌、产品和服务齐发力,东风日产接下来有什么新动作?
linux-mysql-命令
Alibaba cloud Yum source configuration
动态规划【一】(背包问题)
11 Beautiful Soup 解析库的简介及安装
Post Gartner webinar "nine questions on digital transformation"
如何在Chrome浏览器中模拟请求或修改请求的域名
R language uses GLM function to build Poisson regression model, and coef function to obtain the coefficients of Poisson regression model and analyze the effects of various variables
Guys, please ask me a question about flynk SQL. I have an FQL statement, insert into C sale
WMS仓库仓储管理系统源码
尚硅谷 尚硅谷 | 什么是ClickHouse表引擎 Memory和Merge
2022年下半年深圳地区数据分析师认证(CPDA),[进入查看]
What is an SSL certificate and what are the benefits of having an SSL certificate?