当前位置:网站首页>508. Most Frequent Subtree Sum
508. Most Frequent Subtree Sum
2022-06-21 18:06: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).
来源:力扣(LeetCode)
链接: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 %

边栏推荐
猜你喜欢

vivo 容器集群监控系统架构与实践

11 brève introduction et installation de la Bibliothèque d'analyse de soup beautiful

谷歌浏览器80版本以后,如何处理出现的问题SameSite跨域问题

How many correct answers can you get to Huawei Hongmeng certification test questions?

Second cloud's original fully compatible solution for Xinchuang is upgraded to help accelerate the implementation of Xinchuang industry

API interface for discharge summary identification - medical bill OCR identification / discharge diagnosis record / electronic medical record / claim settlement service

Insert class collation

Huawei launches new products again? These models have excellent functions

企评家全面解读:【国家电网】中国电力财务有限公司企业成长性

GoF模式-03-行为型模式(下)
随机推荐
R语言使用epiDisplay包的statStack函数基于因子变量通过分层的方式查看连续变量的统计量(均值、中位数等)以及对应的假设检验
The R language catiols package divides the data, randomforest package constructs the random forest model, uses the importance function to calculate the importance of each feature in the random forest
Mvcc implementation principle of MySQL
SQL operation: with expression and its application
品牌、产品和服务齐发力,东风日产接下来有什么新动作?
Double pointer 1day8 of daily practice of Li Kou
【面试高频题】难度 1.5/5,经典「前缀和 + 二分」运用题
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
轻松入门自然语言处理系列 专题6 代码实战──基于语言模型的拼写纠错
Two ways of encrypting Excel files
GoF模式-03-行为型模式(下)
In the same process of go question bank · 9, what is the problem with sending and receiving data at the same time for unbuffered channels
使用uniapp框架搭建浙里办微应用(单点登录、埋点、适老化、RPC网关)
Qt Creator 7.0常见问题和常见用法
华为鸿蒙认证测试题,你能答对几道?
EasyCVR智能边缘网关硬件如何设置通电自启动?
鸿蒙之后,华为宣布再将捐赠欧拉,鸿蒙和欧拉的捐赠预计将给业界带来哪些影响?
Delete the specified screen
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用coef函数获取模型系数并解析系数意义
[interval and topic prefix sum] line segment tree (dynamic open point) application problem