详见: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

最新文章

  1. 缺省servlet的使用
  2. 100722A
  3. NPOI读取excel功能,兼容xls和xlsx
  4. [ucgui] 对话框4——模式消息窗口
  5. 流量分析 seo alexa 排名
  6. BootStrap2学习日记5---列表
  7. [译]Autoprefixer:用最可行的方式处理浏览器前缀的CSS后处理器
  8. AndroidManifest.xml中的android:name是否带.的区别
  9. 怎样用AIDL Service 传递复杂数据
  10. 图像显示 imshow()[OpenCV 笔记5]
  11. C# 关于NULL 可空值类型 ? 和空接操作符??
  12. JSP入门:介绍什么是JSP和Servlet(转)
  13. xmu1125 越野车大赛(三分)
  14. css之display:inline-block与float区别(可以尝试用一下)
  15. cocos2d-x 定时器selector的使用 :schedule的使用
  16. 老版VC++线程池
  17. python字符串-内置方法用法分析
  18. javaScript(3)---语法、关键保留字及变量
  19. 用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
  20. pymongo基础

热门文章

  1. extjs4 treepanel 多个checkbox先中 多个节点选中 多级节点展开
  2. java反射机制与动态加载类
  3. WinDbg 查看静态变量
  4. Viewpager animation duration setting
  5. ios 图片拉伸方法
  6. SPOJ:Lost and survived(multiset+并查集)
  7. Linux和windows下执行sql脚本文件
  8. MFC程序中的 _T(&quot;&quot;) 什么意思?
  9. 025--python初识类和对象
  10. 关于 android 中 postDelayed方法的讲解 (转载)