LeetCode #508. Most Frequent Subtree Sum

科技中介如何高效匹配技术供需?.docx 科技中介如何高效匹配技术供需? 立即下载

题目描述:

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.

class Solution {
public:
    vector<int> findFrequentTreeSum(TreeNode* root) {
        unordered_map<int,int> count;
        get_subtree_sum(root,count);
        int max_count=0;
        for(auto x:count) max_count=max(max_count,x.second);
        vector<int> result;
        for(auto x:count) if(x.second==max_count) result.push_back(x.first);
        return result;
    }
    
    int get_subtree_sum(TreeNode* node, unordered_map<int,int>& count)
    {
        if(node==NULL) return 0;
        int left=get_subtree_sum(node->left,count);
        int right=get_subtree_sum(node->right,count);
        int sum=left+right+node->val;
        count[sum]++;
        return sum;
    }
};

 

!qhyb.rar 当 CAD 缺失对应字体时,图纸文字会显示异常,出现乱码、问号。将下载好的字体文件复制到 AutoCAD 的 Fonts 文件夹中,即可恢复正常显示。 立即下载

相关推荐

政府科技管理部门如何实现科技成果高效转化?.docx

政府科技管理部门如何实现科技成果高效转化?

leetcode编程记录11 #508 Most Frequent Subtree Sum

leetcode编程记录11 #508 Most Frequent Subtree Sum标签(空格分隔): leetcode这是一道有关树的数据结构的题目,巧妙地利用数据结构可以很快解决这个问题。题目如下: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum

grow_to的博客 199

librdkafka Windows 包(可直接使用)

librdkafka/win ├─ bin/ # 全部运行时dll + pdb调试符号 + plugins插件目录 ├─ include/ # 头文件 rdkafka.h └─ lib/ # .lib静态导入库(链接阶段使用)

leetcode编程记录7 #508 Most Frequent Subtree Sum

leetcode编程记录7 #508 Most Frequent Subtree Sum标签(空格分隔): leetcode今天做的是一道关于树的题,如果利用一些数据结构可以很快和很巧妙地做出来,题目如下:Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of

grow_to的博客 212

[leetcode]508. Most Frequent Subtree Sum(Python)

[leetcode]508. Most Frequent Subtree Sum题目描述题目理解解题思路后序遍历+递归MyMethodTime 题目描述 Category:Medium Tree Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a n...

La_Ragazza的博客 357

Leetcode 508.出现次数最多的子树元素和(Most Frequent Subtree Sum

Leetcode 508.出现次数最多的子树元素和 1 题目描述(Leetcode题目链接)   给你一个二叉树的根结点,请你找出出现次数最多的子树元素和。一个结点的「子树元素和」定义为以该结点为根的二叉树上所有结点的元素之和(包括结点本身)。 你需要返回出现次数最多的子树元素和。如果有多个元素出现的次数相同,返回所有出现次数最多的子树元素和(不限顺序)。 5 / \ 2 -3 返回...

qq_39378221的博客 245

LeetCode508. Most Frequent Subtree Sum【M】【40】

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 (includi

sscssz的博客 620

Leetcode 508. Most Frequent Subtree Sum

递归. 注意:collections.Counter()有一个most_common()函数,比较实用. # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # ...

weixin_30663471的博客 70

LeetCode 508. Most Frequent Subtree Sum

题目描述 Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order. The subtree sum of a node is defined as the sum of all the node values formed by the subtree roote

weixin_43762534的博客 427

LeetCode508. Most Frequent Subtree Sum

问题描述问题链接:https://leetcode.com/problems/most-frequent-subtree-sum/#/descriptionGiven 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

墨染百城 461

LeetCode508. Most Frequent Subtree Sum 解题报告(Python & C++)

LeetCode508. Most Frequent Subtree Sum 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/most-frequent-subtree-sum/description/ 题目描述: Given the root of a tree, you are asked...

负雪明烛 1301

[leetcode]508. Most Frequent Subtree Sum

题目链接:https://leetcode.com/problems/most-frequent-subtree-sum/#/description 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 t

xiaocong1990的博客 275

508. Most Frequent Subtree Sum(python+cpp)

题目: 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 (i...

小湉湉的博客 235

1月8号 503, 504, 506, 506, 508

有点累了,随便写一写吧……都比较简单的题目 进度:-33 文章目录503. Next Greater Element II504. Base 7506. Relative Ranks507. Perfect Number508. Most Frequent Subtree Sum 503. Next Greater Element II 看标题就知道这个题是496. Next Greater El...

qq_42024783的博客 131

国有企业如何推动科技创新与产业升级融合?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

科技中介服务如何实现数字化升级?.docx

科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。

兄弟MFC-J2330DW 中文维修手册

兄弟MFC-J2330DW 中文维修手册

CesiumForUnitySamples-v1.25.1

Cesium for Unity 是一个官方插件,它将 Cesium 强大的3D 地理空间平台能力深度集成到了 Unity 引擎中。简单来说,它让你能在 Unity 里创建出一个高精度的真实地球,并加载和探索全球范围内的海量地理数据

上一篇: LeetCode #456. 132 Pattern
下一篇: #946. Validate Stack Sequences
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值