508 Most Frequent Subtree Sum 出现频率最高的子树和
2024-08-29 05:58:54
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> res;
int cnt=0;
unordered_map<int,int> m;
postorder(root,m,cnt,res);
return res;
}
int postorder(TreeNode* node,unordered_map<int,int> &m,int &cnt,vector<int> &res)
{
if(!node)
{
return 0;
}
int left=postorder(node->left,m,cnt,res);
int right=postorder(node->right,m,cnt,res);
int sum=left+right+node->val;
++m[sum];
if(m[sum]>=cnt)
{
if(m[sum]>cnt)
{
res.clear();
}
res.push_back(sum);;
cnt=m[sum];
}
return sum;
}
};
参考:http://www.cnblogs.com/grandyang/p/6481682.html
最新文章
- 缺省servlet的使用
- 100722A
- NPOI读取excel功能,兼容xls和xlsx
- [ucgui] 对话框4——模式消息窗口
- 流量分析 seo alexa 排名
- BootStrap2学习日记5---列表
- [译]Autoprefixer:用最可行的方式处理浏览器前缀的CSS后处理器
- AndroidManifest.xml中的android:name是否带.的区别
- 怎样用AIDL Service 传递复杂数据
- 图像显示 imshow()[OpenCV 笔记5]
- C# 关于NULL 可空值类型 ? 和空接操作符??
- JSP入门:介绍什么是JSP和Servlet(转)
- xmu1125 越野车大赛(三分)
- css之display:inline-block与float区别(可以尝试用一下)
- cocos2d-x 定时器selector的使用 :schedule的使用
- 老版VC++线程池
- python字符串-内置方法用法分析
- javaScript(3)---语法、关键保留字及变量
- 用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
- pymongo基础