LeetCode 508. Most Frequent Subtree Sum【一开始没看懂题】
来源:互联网 发布:网络宽带装哪家好 编辑:程序博客网 时间:2024/04/29 23:13
Given the root of a tree, you are asked to find the most frequent subtree sum. 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). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.
Examples 1
Input:
5 / \2 -3return [2, -3, 4], since all the values happen only once, return all of them in any order.
Examples 2
Input:
5 / \2 -5return [2], since 2 happens twice, however -5 only occur once.
Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.
#include<iostream>#include<vector>#include<unordered_map>#include<queue>#include<map>using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution {private:map<int, int> m;public:int singleNumber(vector<int>& nums) {//方法1/*unordered_map<int, int> m;for (int i = 0; i < nums.size(); i++){if (m.find(nums[i]) != m.end()){m[nums[i]]++;}else{m.insert({ nums[i], 1 });}}unordered_map<int, int> ::iterator itr = m.begin();while (itr != m.end()){if (itr->second != 2)return itr->first;itr++; }*///方法2int result = 0;for (int i = 0; i < nums.size(); i++){result = result ^ nums[i];}return result;}vector<int> largestValues(TreeNode* root) {//queue<int>q1;queue<TreeNode> q2;q2.push(*root);//q1.push(1);vector<int> ans;while (!q2.empty()){int size = q2.size();int maxval = (numeric_limits<int>::min)();for (int i = 0; i < size; i++){TreeNode treetmp = q2.front();if (treetmp.val > maxval)maxval = treetmp.val; q2.pop();//q1.pop();if (treetmp.left != NULL){q2.push(*treetmp.left);}if (treetmp.right != NULL){q2.push(*treetmp.right);}}ans.push_back(maxval);}return ans;}vector<int> findFrequentTreeSum(TreeNode* root) {vector<int> ans;if (root == NULL)return ans;dfs(root);int maxn = 0;map<int, int>::iterator itr = m.begin();while (itr != m.end()){maxn = max(maxn, itr->second);itr++;}itr = m.begin();while (itr != m.end()){if (itr->second == maxn)ans.push_back(itr->first);itr++;}return ans;}void dfs(TreeNode* root) {if (root == NULL)return;if (root->left != NULL){dfs(root->left);root->val += root->left->val;}if (root->right != NULL){dfs(root->right);root->val += root->right->val;}m[root->val] ++;} };int main(){Solution s;//TreeNode n1(1);//TreeNode n2(3);//TreeNode n3(2);//TreeNode n4(5);//TreeNode n5(3);//TreeNode n6(9);//n1.left = &n2;//n1.right = &n3;//n2.left = &n4;//n2.right = &n5;//n3.right = &n6;//vector<int> v = s.largestValues(&n1);TreeNode n1(5);TreeNode n2(2);TreeNode n3(-5);n1.left = &n2;n1.right = &n3;vector<int> v = s.findFrequentTreeSum(&n1);return 0;}
0 0
- LeetCode 508. Most Frequent Subtree Sum【一开始没看懂题】
- Leetcode-508. Most Frequent Subtree Sum
- [leetcode] 508. Most Frequent Subtree Sum
- LeetCode 508. Most Frequent Subtree Sum
- Leetcode 508. Most Frequent Subtree Sum
- leetcode题解-508. Most Frequent Subtree Sum
- [LeetCode]508. Most Frequent Subtree Sum
- LeetCode笔记:508. Most Frequent Subtree Sum
- 【LeetCode】508. Most Frequent Subtree Sum
- Leetcode 508. Most Frequent Subtree Sum
- leetcode 508. Most Frequent Subtree Sum
- leetcode 508. Most Frequent Subtree Sum
- [leetcode]508. Most Frequent Subtree Sum
- [LeetCode] 508. Most Frequent Subtree Sum
- LeetCode 508. Most Frequent Subtree Sum (c++)
- leetcode 508. Most Frequent Subtree Sum
- leetcode 508. Most Frequent Subtree Sum
- [LeetCode] Most Frequent Subtree Sum
- 用nodejs搭建最简单、轻量化的http server
- window下Slik SVN的安装配置
- 电子商务系统的设计与实现(十一):数据库设计
- 一个简单的Unity游戏开发框架(lua自更新)
- 华为机试在线训练-牛客网(33)矩阵乘法
- LeetCode 508. Most Frequent Subtree Sum【一开始没看懂题】
- 欢迎使用CSDN-markdown编辑器
- Netty系列之Netty高性能之道
- Jfinal自定义实现getModels方法
- 最纯粹的直播技术实战02-Camera的处理以及推流
- C语言基本数据类型
- 【C++】C++ static成员变量与static成员函数
- 【c++】模板实现动态Vector
- HibernateUtils工具类