Week Training: 508 Most Frequent Subtree Sum
来源:互联网 发布:java path环境变量设置 编辑:程序博客网 时间:2024/06/06 01:31
The key to this problem is using STL map. In order to store the occurrence of each sum, we have to add the sum and its occurrence to the map in every traverse, until the tree is fully gone through. Than we just traverse the map, compare every occurrence in the map with the biggest one, and decide which to push into the vector, finally return it.
class Solution {public: vector<int> findFrequentTreeSum(TreeNode* root) { vector<int> values; map<int, int> count; int HighCount = 0; countSum(root, count, HighCount); map<int, int> ::iterator it; for(it=count.begin();it!=count.end();it++){ if(it->second == HighCount) values.push_back(it->first); } return values; } int countSum(TreeNode* root, map<int, int> &count, int &HighCount){ if(root==NULL)return 0; int x = root->val; x += countSum(root->left, count, HighCount); x += countSum(root->right, count, HighCount); count[x]++; HighCount = max(HighCount, count[x]); return x; }};
0 0
- Week Training: 508 Most Frequent Subtree Sum
- Most Frequent Subtree Sum
- Most Frequent Subtree Sum
- [leetcode 508]Most Frequent Subtree Sum
- leetcode 508 Most Frequent Subtree Sum C++
- LeetCode.508 Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- leetcode508 Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- 508. Most Frequent Subtree Sum
- [LeetCode] Most Frequent Subtree Sum
- 【软件工程】机房文档--概要设计说明书
- 记一道读文件的题(按行读,split(),atoi())
- 一个Python小白5个小时爬虫经历
- Scrapy 安装全过程(在windows的python3中)
- 历届试题 矩阵翻硬币
- Week Training: 508 Most Frequent Subtree Sum
- CAS实现SSO单点登录原理
- AS: Unsupported method: AndroidProject.getPluginGeneration().
- CodeForces
- [转]PHP7内核剖析之变量的内部实现
- MySchool 07 上机
- wdcp组件安装
- PAT 1005
- PHP字符串函数