[Leetcode] 652. Find Duplicate Subtrees 解题报告

Agent 操作系统:未来的 Harness 平台 在大模型技术爆发的当下,自主智能体(Agent)已经成为AI原生应用的核心载体,但当前Agent开发面临着碎片化严重、运维难度高、规模化落地难三大核心痛点,就像1970年代没有通用操作系统的裸机编程时代,开发者需要重复造轮子解决底层基础设施问题。本文将深度解析Agent操作系统(Agent OS)作为AI原生时代的底层基础设施的核心定位,以及下一代Harness平台如何将Agent OS与成熟的DevOps、CI/CD、可观测性、成本管理能力深度融合,成为AI原生应用全生命周期管理的统一平台。 阅读详情

题目:

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof them.

Two trees are duplicate if they have the same structure with same node values.

Example 1: 

        1
       / \
      2   3
     /   / \
    4   2   4
       /
      4
The following are two duplicate subtrees:
      2
     /
    4
and
    4
Therefore, you need to return above trees' root in the form of a list.

思路:

发现利用序列化是个非常好的思路:子树相同的充要条件是其序列化结果也相同。因此我们定义一个从string到vector<TreeNode*>的哈希表,然后对每个非空节点进行序列化,并将结果加入哈希表中。如果某个string对应的数组大小大于1,就将其数组中的一个元素加入结果集中。这样算法的时间复杂度只有O(n)。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        unordered_map<string, vector<TreeNode*>> hash;
        serialize(root, hash);
        vector<TreeNode*> ret;
        for (auto it = hash.begin(); it != hash.end(); ++it) {
            if (it->second.size() > 1) {
                ret.push_back(it->second[0]);
            }
        }
        return ret;
    }
private:
    string serialize(TreeNode *node, unordered_map<string, vector<TreeNode*>> &hash) {
        if (!node) {
            return "";
        }
        string s = "(" + serialize(node->left, hash) 
                       + to_string(node->val) 
                       + serialize(node->right, hash) + ")";
        hash[s].push_back(node);
        return s;
    }
};
手把手教你用TP4056和PMOS管搭建锂电池自动切换电路(含元器件选型指南) 本文详细介绍了如何使用TP4056和PMOS管搭建锂电池自动切换电路,包括核心架构设计、元器件选型指南和PCB布局优化。通过智能充电算法和电源路径切换模块,实现外部供电与锂电池的无缝切换,提升便携式设备的电源管理效率。 阅读详情

相关推荐

从YOLOv8到RTDETR:如何将训练后的YOLO指标无缝转换为COCO格式

本文详细介绍了如何将训练后的YOLOv8指标无缝转换为COCO格式,特别适用于RTDETR模型的评估。通过数据准备、指标转换流程和实际项目经验,帮助开发者解决YOLO与COCO指标差异问题,提升模型评估的准确性和权威性。

weixin_29085151的博客 383

重复子树查找(Find Duplicate Subtrees)

参考:https://blog.csdn.net/CsWarmSun/article/details/112410914

qq_24505417的博客 461

Jupyter Lab 无法启动 Kernel 问题排查与解决总结

使用 curl,telnet,或 Python websocket-client 库可以本地模拟 WebSocket 握手行为,帮助分析服务端响应状态。避免使用 http:// + ws:// 组合方式部署 Jupyter Lab,特别是在远程或跨域访问场景。建议统一采用 https:// + wss:// 方式部署,确保浏览器信任并兼容。确认 WebSocket 已升级为 wss://,内核可正常启动。但在远程访问时,浏览器出于安全策略,默认拒绝 ws:// 协议。

weixin_43866043的博客 1873

Leetcode 652. Find Duplicate Subtrees 寻找重复子树 解题报告

这道题,给了一个二叉树,然后需要找出所有重复的子树(注意这个子树返回一个就可以了)做法naive一点可以记录每个node的值,然后比较相同的值的遍历。。 进阶一点的话,可以记录以每个节点为开始时的先序遍历(中左右)的数值,然后遍历的序列如果重复出现了,那么就自然可以加入了以上问题等价于: 记录每一颗子树的先序/后序便利结果,然后比较看有没有重复的,有重复的就可以加入PS:后续遍历也可以,只要别让

MebiuW的专栏 4430

MT4/MT5数据源接入全攻略:从买服务、自研API到低成本DDE,哪种方案适合你?

本文全面解析MT4/MT5数据源接入的三种主流方案:商业服务、自研API和低成本DDE,详细对比其技术参数、成本结构和适用场景。帮助技术团队根据业务需求选择最优方案,确保外汇交易系统的数据流稳定性和实时性,提升交易体验和业务连续性。

weixin_42568801的博客 182

TwinCAT3实战:从零搭建倍福PLC的Modbus-TCP Server

本文详细介绍了如何使用TwinCAT3从零搭建倍福PLC的Modbus-TCP Server,包括环境准备、工程配置、参数设置、客户端连接测试及常见问题排查。通过实战步骤和实用技巧,帮助工程师快速掌握倍福PLC与Modbus-TCP的通信实现,提升工业自动化项目的开发效率。

weixin_30797199的博客 186

leetcode 652. Find Duplicate Subtrees(找出重复的子树)

Given the root of a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the same structure with the same node values. Example 1: In

蓝羽飞鸟的博客 269

睿抗机器人魔力元宝在完成任务过程中如何使机械臂先到达白色盒子上方而不是先到达工作台?

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1388

Leetcode 652. 寻找重复的子树

Leetcode 652. 寻找重复的子树

weixin_44852067的博客 440

【30】软考软件设计师——UML类图与用例图满分精讲|下午第3题常考核心

本文是《软件设计师·50讲通关|从零基础到工程师职称》专栏第30篇,聚焦**模块四:应用技术(下午题)第3道高频大题**,UML建模是历年下午必考核心,单题分值稳定10~12分。全文深度拆解两大核心UML图表:**类图**与**用例图**,超详细讲解类图三层结构、可见性修饰符、五大核心关系(泛化/关联/依赖/聚合/组合)的判定标准与绘图规范;吃透用例图参与者、系统边界、三大关系(包含/扩展/泛化)的底层逻辑,搭配独家**关系秒杀口诀、标准绘图模板、真题改错技巧**;全文适配补图、判断关系、分析场景、纠错优化

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 584

1️⃣ Coze智能体基础入门教学(2025年全新版本)

不妨设想一下,倘若我们能够打造出一个数字助手,它不但能精准洞悉我们的需求,更可以独立思索、学习以及付诸行动,这便是 AI Agent 的基础理念。它恰似计算机世界里的 “智慧精灵”,能够感知周遭环境、做出判断并采取行动。AI Agent 的发展之路精彩纷呈。它走过了三个重要阶段:起初,它犹如一个被规则束缚的机器人,只能按预设好的指令开展工作。接着,它学会运用统计学方法,通过分析数据来做决策,变得越发灵活。到如今,凭借深度学习技术,它已经能够像人类一样去学习复杂的任务了。

李同学Lino的博客 4112

Leetcode刷题652. 寻找重复的子树

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。对于同一类的重复子树,你只需要返回其中任意。来源:力扣(LeetCode)给你一棵二叉树的根节点。

Bonbon_wen的博客 407

英伟达 Jetson AGX Thor 官方入门教程(二)CUDA、Docker、JetPack 设置

本文详细介绍了NVIDIA Jetson AGX Thor开发套件的安装与配置流程。主要内容包括:1) BSP安装的三种方法(Jetson ISO、SDK Manager和Linux刷写脚本)及其适用场景;2) Docker环境的完整配置步骤及测试方法;3) CUDA工具包的两种安装方式(容器化和原生安装)及其性能对比;4) JetPack SDK的安装选项。文章提供了详细的命令行操作指南和故障排除建议,帮助开发者快速搭建AI开发环境,充分发挥Jetson平台的硬件性能优势。

机器人控制与仿真应用 1763

【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

【LeetCode】652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/find-dup...

负雪明烛 2520

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...

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode //C - 652. Find Duplicate Subtrees

【代码】LeetCode //C - 652. Find Duplicate Subtrees。

Made in Code 1270

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

[leetcode]652. Find Duplicate Subtrees(Python)

[leetcode]652. Find Duplicate Subtrees题目描述题目理解解题思路Time 题目描述 Category:Mediudm Tree Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the ...

La_Ragazza的博客 394

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

[LeetCode] 652. Find Duplicate Subtrees

原题链接:https://leetcode.com/problems/find-duplicate-subtrees/ 1. 题目介绍 Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of an...

Ber03的博客 369

[LeetCode 652] Find Duplicate Subtrees

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of anyoneof them. Two trees are duplicate if they have the same struct...

ExcitedZhang的博客 213

Leetcode 652 Find Duplicate Subtrees

Problem: Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the sam...

Opium_Z的博客 194

[leetcode] 652. Find Duplicate Subtrees

Description Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the ...

Learning from the mistakes 280

LeetCode 652. Find Duplicate Subtrees【Java】

题目描述 652. 寻找重复的子树 AC代码 /* 解题思路: 1.首先进行序列化成字符串,然后统计字符串中字串出现的次数,输入重复的即可。 2.时间复杂度为O(n),两遍hash. */ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode lef...

NayelyA的博客 234

[leetcode]652. Find Duplicate Subtrees

题目链接:https://leetcode.com/problems/find-duplicate-subtrees/description/ Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the ro

xiaocong1990的博客 1376

java found dulicate_LeetCode 652: 寻找重复的子树Find Duplicate Subtrees

LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees题目:给定一棵二叉树,返回所有重复的子树。对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。两棵树重复是指它们具有相同的结构以及相同的结点值。Given a binary tree, return all duplicate subtrees. For each kind of duplica...

weixin_36024234的博客 172

[leetcode] 652. Find Duplicate Subtrees @ python

原题 Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the same stru...

闲庭信步 229

LeetCode 652. Find Duplicate Subtrees

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the same structu...

zhaohaibo的博客 450

LeetCode #652 - Find Duplicate Subtrees

题目描述: Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of anyoneof them. Two trees are duplicate if they have the same ...

LawFile的博客 235
上一篇: [Leetcode] 651. 4 Keys Keyboard 解题报告
下一篇: [Leetcode] 653. Two Sum IV - Input is a BST 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址