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. 70道阿里百度高频Java面试题(框架+JVM+多线程+算法+数据库)

    基础与框架 1.String类能被继承吗,为什么 2.String,Stringbuffer,StringBuilder的区别? 3.ArrayList和LinkedList有什么区别 4.类的实例化 ...

  2. ubuntu远程桌面软件vnc。

    http://www.linuxidc.com/Linux/2017-03/141936.htm 现在Linux是非常火的  很多人喜欢用它,界面简单,操作容易,有很多图形化工具如WinSCP可以与U ...

  3. 牛客练习赛31A 地、颜色、魔法(搜索+二维数组一维表示)

    红色来源于山脉,象征着狂躁.愤怒.混乱,血雨腥风,电光火石. 蓝色来源于海岛,象征着控制.幻觉.诡计,运筹帷幄,谋定后动. 绿色来源于树林,象征着生命.蛮力.成长,横冲直撞,生生不息. 黑色来源于沼泽 ...

  4. 在.NET Core控制台中使用依赖注入

    本文介绍如何在控制台应用程序中使用微软提供的依赖注入功能,掌握控制台中的用法后,可以扩展到构建windows服务中. 创建控制台应用程序 添加DependencyInjection的引用 Instal ...

  5. SpringBoot SpringCloud 热部署 热加载 热调试

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...

  6. 程序员offer沟通的4个基本原则

     常柱 架构未来  你需要就一份新工作进行沟通时:比如你的薪水.福利,或者我个人最喜欢的每周工作时间缩短等,当公司问你“你想要多少?”或者“这是我们的报价,你说呢?” 最后关于薪资的谈话可能是最伤脑筋 ...

  7. 脚本批量执行Redis命令

    1.将命令写在文件中 数据量比较大的话,建议用程序去生成文件.例如: List<String> planIdList = planDao.findAll().parallelStream( ...

  8. 解决mysql导入数据量很大导致失败及查找my.ini 位置(my.ini)在哪

    数据库数据量很大的数据库导入到本地时,会等很久,然而等很久之后还是显示失败: 这是就要看看自己本地的没mysql是否设置了超时等待,如果报相关time_out这些,可以把mysql.ini尾部添加ma ...

  9. centos7网口添加IP,修改默认路由永久地址生效

    1永久增加ip地址和路由 网卡永久添加ip地址 注释:ens192为管理地址网卡,请根据实际情况进行修改,网关以192.168.160.1为例 复制一份网卡配置文件命名为ifcfg-ens192:1 ...

  10. 最新IDEA永久激活

    此教程已支持最新2019.2版本 本教程适用Windows.Mac.Ubuntu等所有平台. 激活前准备工作 配置文件修改已经不在bin目录下直接修改,而是通过Idea修改 如果输入code一直弹出来 ...