码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode知识点总结 - 508


    LeetCode 508. Most Frequent Subtree Sum

    考点难度
    DFSMedium
    题目

    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 rooted at that node (including the node itself).

    思路

    用hashmap储存sum出现的次数,DFS所有subtree

    答案
    class Solution:
        def findFrequentTreeSum(self, root):
            if root is None: return []
    
            def dfs(node):
                if node is None: return 0
                s = node.val + dfs(node.left) + dfs(node.right)
                count[s] += 1
                return s
    
            count = collections.Counter()
            dfs(root)
            maxCount = max(count.values())
            return [s for s in count if count[s] == maxCount]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    关于在WPF xaml中包含另一个window的方法
    【宠物用品】宠物饮水机方案
    【云原生 | Kubernetes 系列】---Prometheus 监控Java服务
    cudann官网上不见了,找不到了安装CUDA和CUDANN时,发现CUDANN不见了,官网甚至找不到一丝它的信息,公告也没有
    深入解析JavaScript中的变量作用域与声明提升
    国产数据库人大金仓V8
    GraphQL 进阶——DataLoader
    kubernetes 初始化
    C语言实现循环双链表
    D-Wave公开演示大规模相干量子退火
  • 原文地址:https://blog.csdn.net/m0_59773145/article/details/128022516
  • 最新文章
  • Vector 选型与实战:vs OTel / Logstash / Fluentd 全维对比,及统一日志与指标管道的 AWS ECS 落地
    开源分享|用MicroPython 做了个 AI 小鸡,它会长大,还记得我所有的情绪
    5 分钟上手 AgentRun:从注册到第一个 Agent 运行
    transformer进阶之路:#4 attention从如何「工作」到「为何」如此高效
    大模型推理的“两步走”:Prefill 与 Decode 全流程科普详解
    让 FastAPI Agent 思考不阻塞:手把手教你实现异步任务与后台处理方案
    BMAD Story Automator 上手实录:把 5 个待办 Story 交给 AI 自主推进
    自己用 ai 写了个链接 mysql 数据库的 mcp 工具
    用ANTLR实现表达式词法和语法分析器
    Claude Code安装,接入阿里云百炼模型,蹭蹭免费额度
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号