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

Allegro如何进行四层板板层设计 板层的设计 阅读详情

题目:

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;
    }
};
基于强化学习控制策略的三相逆变器仿真 强化学习控制器的核心在于定义状态空间、动作空间、奖励函数以及选择合适的强化学习算法(例如DQN、DDPG等)。对于三相逆变器,状态可以是输出电压、电流等信息;动作则可能是调制指数的变化量;奖励函数则应鼓励快速准确地跟踪参考信号。定义状态空间和动作空间状态空间可以包括逆变器的输出电压、电流及其变化率等信息。动作空间则是控制器可执行的动作,例如PWM信号的调整幅度。matlab深色版本% 定义状态和动作空间维度numObs = 6;% 假设有六个状态变量(例如三相电压和电流) 阅读详情

相关推荐

Mediapipe姿态估计——用坐标计算手指关节弯曲角度并实时标注

利用mediapipe搭建姿态检测模型;mediapipe人体标注节点坐标;mediapipe计算手指弯曲角度并实时渲染;mediapipe计算关节弯曲角度。mediapipe手势识别;mediapipe姿态检测模型;mediapipe学习。

kalakalabala的博客 1万+

重复子树查找(Find Duplicate Subtrees)

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

qq_24505417的博客 461

通过python下载西门子技术中心文档

本篇文章纯粹记录如何下载通过python下载西门子技术中心文档,所下载文档完全自己学习使用,非商业用途

John_09040211的博客 561

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

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

MebiuW的专栏 4428

手把手教你用GPIO模拟MDIO时序,搞定Linux下那些‘不听话’的PHY芯片

本文详细介绍了如何利用Linux内核的`virtual,mdio-gpio`驱动,通过GPIO模拟MDIO时序来解决特殊PHY芯片的驱动调试问题。从硬件准备、设备树配置到驱动实现,提供了完整的解决方案和实战案例,帮助开发者应对非标准时序的PHY芯片挑战。

weixin_29188043的博客 270

Jetpack Compose Navigation实战:从基础到高级导航模式

本文全面解析Jetpack Compose Navigation在Android开发中的应用,从基础导航实现到高级模式(如单任务/单顶模式),涵盖参数传递、嵌套导航、深层链接等核心功能。通过实战代码示例展示如何优化导航性能,并与ViewModel、Hilt等架构组件整合,为开发者提供高效的Compose导航解决方案。

pz890123456的博客 372

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

蓝羽飞鸟的博客 267

VHDL语法学习笔记:一文掌握VHDL语法

今天给大侠带来FPGA 之 VHDL 语法学习笔记,话不多说,上货。 VHDL语法学习笔记 一、VHDL简介 1.1 VHDL 的历史 VHDL 的 英 文 全 名 是 Very-High-Speed Integrated Circuit Hardware DescriptionLanguage,诞生于 1982 年。 1987 年底,VHDL 被 IEEE 和美国国防部确认为标准硬件描述语言。自 IEEE 公布了 VHDL 的标准版本 IEEE-1076(简称 87 版)...

2万+

Leetcode 652. 寻找重复的子树

Leetcode 652. 寻找重复的子树

weixin_44852067的博客 437

【ROS机器人系统】实验4 ROS简单应用:激光SLAM与导航

ROS简单应用:激光SLAM与导航 (1)了解机器人和SLAM之间关系 (2)了解激光SLAM (3)了解gmapping和导航

南京大学软件工程硕士在读 2146

【深度学习】脸部修复,CodeFormer,论文,实战

Blind face restoration是一个高度 ill-posed 的问题,通常需要辅助引导来 1) 改善从降级输入到期望输出的映射,或者 2) 补充在输入中丢失的高质量细节。在本文中,我们展示了在一个小的代理空间中学到的离散码本先验可以通过将盲目的面部恢复建模为 a code prediction task来大大减少恢复映射的不确定性和模糊性,同时为生成高质量面部提供丰富的视觉元素。

q742971636的博客 2994

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

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

Bonbon_wen的博客 407

小米汽车车载多摄像头视频链路深度解析 | MAX96792/MAX96724/MAX96781 实战分享

通过合理的输入输出规划 + 虚拟通道管理 + 加串行策略,可以在成本和性能之间取得最佳平衡,这就是多摄像头系统设计的核心精髓。

weixin_43199439的博客 1547

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

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

负雪明烛 2516

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode //C - 652. Find Duplicate Subtrees

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

Made in Code 1267

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

[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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[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的博客 368

[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的博客 193

[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 278

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的博客 231

[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的博客 1374

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

闲庭信步 226

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的博客 449

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、付费专栏及课程。

余额充值