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 structure with same node values.

示例 1:

        1
/ \
2 3
/ / \
4 2 4
/
4

下面是两个重复的子树:

      2
/
4

    4

因此,你需要以列表的形式返回上述重复子树的根结点。

Therefore, you need to return above trees' root in the form of a list.

解题思路:

这就是一道考察二叉树遍历的题, 遍历的时候序列化作为 String 类型存入 Map, 若其为第二次出现即将该结点加入数组.

代码:

这里以后序遍历为例, 需要注意叶子结点应当规定一个特殊字符作为替代 null 结点, 这里用的是 '#'

Java:

class Solution {
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> list = new ArrayList<>();//待返回数组
if (root == null) return list;
Map<String, Integer> map = new HashMap<>();//哈希映射查找重覆子结点
traverse(root, map, list, "");
return list;
}
//后序遍历
private String traverse(TreeNode node, Map<String, Integer> map, List<TreeNode> list, String tree) {
if (node == null) return tree + "#";
String left = traverse(node.left, map, list, tree);//递归左子孩子
String right = traverse(node.right, map, list, tree);//递归右子孩子
tree = tree + node.val + left + right;//每个子树路径 map.put(tree, map.getOrDefault(tree, 0) + 1);//存储每个子树路径
if (map.get(tree) == 2) list.add(node);//第二次出现相同路径时存储该结点
return tree;
}
}

Python:

class Solution:
def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:
res = [] # 待返回数组
if not root: return res
hash_map = {} # 哈希映射查找重覆子结点
self.traverse(root, hash_map, res, '')
return res
# 后序遍历
def traverse(self, node: TreeNode, hash_map, res, tree):
if not node: return tree + '#'
left = self.traverse(node.left, hash_map, res, tree) # 递归左子孩子
right = self.traverse(node.right, hash_map, res, tree) # 递归右子孩子
tree = tree + str(node.val) + left + right # 每个子树路径
if tree in hash_map.keys(): # 存储每个子树路径
hash_map[tree] += 1
else:
hash_map[tree] = 1
if hash_map[tree] == 2: # 第二次出现相同路径时存储该结点
res.append(node)
return tree

欢迎关注微信公众号: 爱写Bug

LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees的更多相关文章

  1. Java实现 LeetCode 652 寻找重复的子树(两个map的DFS)

    652. 寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 示例 1: 1 / \ ...

  2. Leetcode 652.寻找重复的子树

    寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需 ...

  3. [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  4. 寻找重复的子树 Find Duplicate Subtrees

    2018-07-29 17:42:29 问题描述: 问题求解: 本题是要求寻找一棵树中的重复子树,问题的难点在于如何在遍历的时候对之前遍历过的子树进行描述和保存. 这里就需要使用之前使用过的二叉树序列 ...

  5. LeetCode 219: 存在重复元素 II Contains Duplicate II

    题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. ​ Given an ...

  6. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  7. 652. Find Duplicate Subtrees找出重复的子树

    [抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...

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

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)

    Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和  ...

随机推荐

  1. 非线性函数的最小二乘拟合及在Jupyter notebook中输入公式 [原创]

    突然有个想法,能否通过学习一阶RC电路的阶跃响应得到RC电路的结构特征——时间常数τ(即R*C).回答无疑是肯定的,但问题是怎样通过最小二乘法.正规方程,以更多的采样点数来降低信号采集噪声对τ估计值的 ...

  2. .NET Core环境变量和用户秘钥实现开发中的数据安全

    目录 一.注入 IConfiguration 二.从配置文件 appsettings.json 中获取环境变量 三.从项目中获取环境变量 四.用户秘钥设置环境变量 前言:有很多人将秘钥,数据库连接字符 ...

  3. webpack学习_管理输出(管理资源插件)

    管理输出步骤 Step1:在src新建文件print.js添加逻辑 Step2:在src/index.js import 引用新添加的逻辑 Step3:更新dist/index.html文件,修改引入 ...

  4. LinuxMint自定义快捷键

    如图

  5. 牛客NOIP暑期七天营-提高组5+普及组5

    ————提高组———— 第一题:deco的abs 题目链接:https://ac.nowcoder.com/acm/contest/934/A 因为每个数都可以加任意次 d ,所以可以推出 0 < ...

  6. mysql过期修改

    1.打开cmd 2.链接数据库如 mysql -h localhost -P 3306 -u root -proot 3.修改密码 mysql < set password for 用户名@lo ...

  7. Python 浮点数的冷知识

    本周的PyCoder's Weekly 上分享了一篇小文章,它里面提到的冷知识很有意思,我稍作补充,分享给大家. 它提到的部分问题,读者们可以先思考下: 若两个元组相等,即 a==b 且 a is b ...

  8. 在.Net Core中记录日志

    一个完善的系统,必然会有非常完善的日志记录,用户的操作.系统的运行状况等信息被完整的记录下来,方便我们对系统进行维护和改进..net core 也为日志记录提供了内置的支持. 在控制台程序中记录日志 ...

  9. javascript for循环+异步请求导致请求顺序不一致

    工作中遇到一个问题 for循环,再把循环出来的ID再进行二次请求 这就导致一个问题 请求结果返回顺序不一致 原因:异步请求会把回调事件放入微任务事件队列,宏任务执行完毕再执行微任务,具体参考事件队列机 ...

  10. ffmpeg+nginx 实现rtsp转rtmp并通过nginx转发

    Windows安装 ffmpeg ffmpeg windows版下载地址https://ffmpeg.zeranoe.com/builds/ static版本就行 配置环境变量:下载的压缩包解压后的路 ...