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   -3
return [2, -3, 4], since all the values happen only once, return all of them in any order.

Examples 2
Input:

  5 /  \2   -5
return [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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果平板卡机了怎么办 苹果7plus掉漆怎么办 皮的手机壳脏了怎么办 小米5x边边裂开怎么办 荣耀8的后盖摔了怎么办 hp打印机卡了纸怎么办 华为荣耀9进水了怎么办 小米5x屏幕脱胶怎么办 小米5x后盖松动怎么办 苹果手机没电了怎么办 荣耀9的后盖裂了怎么办 手机一直在开机画面怎么办华为 华为手机一直显示开机画面怎么办 华为p7手机开不了机怎么办 华为荣耀8弯了怎么办 手机壳掉漆了怎么办 华为5a手机音量小怎么办 华为5a手机声音小怎么办 苹果屏幕磨花了怎么办 白色磨砂手机壳脏了怎么办 胶皮手机壳变黄怎么办 手机壳边缘黑了怎么办 手机壳磨黑了怎么办 iphon8原装后壳碎裂怎么办 皮的手机壳发黄怎么办 荣耀手机一直在开机画面怎么办 玻璃手机壳碎了怎么办 华为6x信号不好怎么办 昂达平板v819i刷成砖了怎么办 华为5x忘记密码怎么办 荣耀8开不了机怎么办 华为5s死机了怎么办 华为重启后忘了解锁密码怎么办 华为mate.9上网速度慢怎么办 华为mate10上网速度慢怎么办 四核豌豆2变砖怎么办 苹果32g不够用怎么办 移动4g网络卡怎么办 移动4g网非常卡怎么办 移动4g卡网速慢怎么办 手机移动4g网卡怎么办