【LeetCode】508. Most Frequent Subtree Sum【M】【40】

2025年普通人转向人工智能运维(AIOps)学习建议(附最新技术实践与资源) AIOps不仅是技术的叠加,更是运维流程的重构。建议通过“学习-实践-反馈”循环,逐步从工具使用者成长为解决方案设计者。如需完整资源链接,可访问上述推荐的优快云博客及GitHub仓库。 阅读详情

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.

Subscribe to see which companies asked this question.

这个用递归做还是很舒服的,而且就是标准的递归应用场景,对每个节点算sum,然后字典排序就行了



# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findFrequentTreeSum(self, root):
        if not root:
            return []
        dic = {}

        def calSum(root):
            if root == None:
                return 0
            s = calSum(root.left) + calSum(root.right) + root.val
            dic[s] = dic.get(s,0) + 1
            return s

        calSum(root)

        l = sorted(dic.iteritems(),key = lambda k:k[1],reverse = True)

        maxx = l[0][1]

        res = []
        for k,v in l:
            if v == maxx:
                res += k,
            else:
                break
        return res
        """
        :type root: TreeNode
        :rtype: List[int]
        """


2025.05.30【转录组】|Ribo-seq数据流程详解(一 质量控制) 本文详细介绍了Ribo-seq数据的质控分析流程,重点讲解了使用fastp进行原始数据质控的关键步骤和参数设置。作者通过Snakemake自动化流程示例,展示了如何去除接头序列、过滤低质量reads、生成质控报告等操作。文章强调高质量的Ribo-seq数据对转录后调控和翻译动态研究的重要性,并提供了兼顾数据质量和下游分析需求的参数配置建议。该流程有助于研究人员规范高效地完成Ribo-seq数据质控,为后续分析提供可靠数据基础。 阅读详情

相关推荐

基于FPGA的 SelectIO IP核 的仿真验证-----( LVDS差分高速传输)

SelectIO IP核 的仿真验证

qq_43618170的博客 1万+

103. Binary Tree Zigzag Level Order Traversal【M】

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary

sscssz的博客 485

05、基于ALV表报实现复选框、全选、取消全选等功能

基于Function ALV实现复选框、全选及取消全选等功能

wo520yl1314的博客 2370

Leetcode【111】Minimum Depth of Binary Tree(Python版)

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def minDepth(sel...

ssjdoudou的博客 7054

AI报告撰写实战指南:从提示词工程到全流程优化的底层逻辑与实践突破

传统AI写作常被诟病为“信息拼贴”——模型虽能生成文本,却常出现逻辑断裂、数据矛盾、风格不符等问题。而“全流程优化”则是通过系统化设计,将报告生成拆解为需求解析→数据对齐→提示词设计→初稿生成→迭代优化→格式适配六大环节,每个环节均通过技术手段提升输出质量。提示词工程:本质是通过结构化指令(如角色设定、任务分解、格式约束、示例引导)精准控制模型输出方向,是连接用户需求与模型能力的“翻译器”。全流程优化。

2501_92808811的博客 884

DSP配置I2C通讯(非IO口软件模拟时序)

dsp即数字信号处理器,是一种广泛应用的嵌入式处理器,主要应用是实时快速地实现各种数字信号处理算法,目前,国际主要的dsp供应商是ti公司,其tms32系列产品占据了dsp市场近一半的份额,本文以TMS320F28377D为例,使用ti公司dsp开发工具ccs10.1配置i2c模块。 写这篇文章的缘由,因为市面上很多DSP其实内部是具有硬件I2C接口的,但由于网络上大部分资料和例程都是利用IO口模拟I2C时序进而实现I2C通讯的。给很多想学习硬件I2C通讯的小伙伴带来了困难。 下面先介绍一下配置步骤 1.E

weixin_44387442的博客 5544

[Swift]LeetCode508. 出现次数最多的子树元素和 | Most Frequent Subtree Sum

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30367945的博客 148

【2026年米哈游春招 - 3月14日 -第一题- 行列相等】(题目+思路+Java&C++&Python解析+在线测试)

摘要 本文解决了一个矩阵行列和匹配问题:统计矩阵中行和等于列和的所有行列对数量。算法首先计算每行和每列的元素和,然后利用哈希表统计各行列和的出现次数。通过遍历列和并查询对应的行和出现次数,高效计算出匹配对数。该方法时间复杂度为O(nm),空间复杂度为O(n+m),适用于大规模矩阵数据。代码实现了Python、Java和C++版本,通过哈希表优化避免了暴力枚举,显著提高了计算效率。

m0_73659489的博客 83

【leetcode】447. Number of Boomerangs【E】

Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between iand j equals the distance between i and k (the order of t

sscssz的博客 2462

基于Python的海关进出口数据爬取与贸易关系分析

本文介绍了如何使用Python爬虫技术获取海关进出口数据,并基于这些数据进行贸易关系分析。文章详细阐述了数据采集、清洗、存储和分析的全过程,使用了最新的爬虫技术和数据分析方法,为国际贸易研究提供数据支持和技术参考。关键词:Python爬虫、海关数据、贸易分析、数据可视化、Selenium、Scrapy。

2201_76125261的博客 543

【ArcGIS Pro微课1000例】0073:ArcGIS Pro3.5.2学习版下载及安装教程

ArcGIS Pro 3.5.2安装教程摘要:系统需Windows10及以上,8GB内存(推荐32GB)。安装步骤:1)安装.NET 8.0运行环境;2)执行主程序安装包,按向导完成基础安装;3)安装升级补丁;4)替换AfCore.dll文件并导入注册表。注意安装后删除临时文件节省空间。软件下载地址包含在百度网盘链接。安装过程需依次完成环境配置、主程序安装、补丁更新及破解步骤。

智慧空间实验室 1771

【leetcode】378. Kth Smallest Element in a Sorted Matrix【M】【88ms】

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not t

sscssz的博客 1865

【个人成长笔记】在Ubuntu中安装RTL8188GU USB无线网卡驱动(Driver for Linux RTL8188GU)

这篇笔记提供了在Ubuntu系统中安装RTL8188GU无线网卡驱动的完整指南。主要内容包括:驱动程序的官方来源(Github链接)、详细安装步骤(通过git下载、编译安装等命令),以及将设备转换为USB网卡模式的关键指令。特别提醒每次开机都需要执行模式转换命令,因为设备默认会以U盘模式启动。文章以简洁的技术文档形式呈现,适合Linux用户快速查阅和操作。

yuhuasheng1997的博客 1824

【leetcode】398. Random Pick Index【M】

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size c

sscssz的博客 1398

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【leetcode】438. Find All Anagrams in a String【E】

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be lar

sscssz的博客 1391

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【LeetCode】515. Find Largest Value in Each Tree Row【E】【87】

You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9] Subsc

sscssz的博客 1367

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

203. Remove Linked List Elements【E】【70】【leetcode】

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,  val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits: Special thank

sscssz的博客 751

341. Flatten Nested List Iterator【M】【42】

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1: Given the list [

sscssz的博客 687

【leetcode】146. LRU Cache【H】【9】

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if

sscssz的博客 619

105. Construct Binary Tree from Preorder and Ignorer Traversal【M】【35】【再来一遍】

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Subscribe to see which companies asked this q

sscssz的博客 610

1. Two Sum【E】【59+25】【leetcode】

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums =

sscssz的博客 584

【LeetCode】451. Sort Characters By Frequency【M】【68】

Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e

sscssz的博客 527

347. Top K Frequent Elements【M】【VIP】【字典排序】

Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note:  You may assume k is always valid, 1 ≤ k ≤ number

sscssz的博客 519

49. Group Anagrams 【M】【47】

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],  Return: [   ["ate", "eat","tea"],   ["nat","tan"],   ["bat"] ] Note:

sscssz的博客 437

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 112

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 365
上一篇: 【LeetCode】442. Find All Duplicates in an Array【M】【60】
下一篇: 【LeetCode】520. Detect Capital【E】【83】
Vicky_Vicky_990121
博客等级 码龄14年 23粉丝 187原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值