LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
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的更多相关文章
- Java实现 LeetCode 652 寻找重复的子树(两个map的DFS)
652. 寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 示例 1: 1 / \ ...
- Leetcode 652.寻找重复的子树
寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需 ...
- [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 寻找重复的子树 Find Duplicate Subtrees
2018-07-29 17:42:29 问题描述: 问题求解: 本题是要求寻找一棵树中的重复子树,问题的难点在于如何在遍历的时候对之前遍历过的子树进行描述和保存. 这里就需要使用之前使用过的二叉树序列 ...
- LeetCode 219: 存在重复元素 II Contains Duplicate II
题目: 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. Given an ...
- [LeetCode] Find Duplicate Subtrees 寻找重复树
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 652. Find Duplicate Subtrees找出重复的子树
[抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
随机推荐
- dapr微服务.netcore sdk入门
Actors入门 先决条件 .Net Core SDK 3.0 Dapr CLI Dapr DotNet SDK 概述 本文档描述如何在客户端应用程序上创建Actor(MyActor)并调用其方法. ...
- java基础篇一
引言 本人系南京一小小学校的大三小小菜鸟,三年来学了很多杂七杂八的,也荒废了大量的时间,马上就要秋招了,之前也看了不少面试题,备选了一些简单的项目,看了不知多少本的几百页厚的各种知识的pdf电子书,发 ...
- C++程序设计实验考试准备资料(2019级秋学期)
程序设计实验考试准备资料 ——傲珂 #include<bits/stdc++.h> C++常用函数: <math.h>头文件 floor() 函数原型:double floor ...
- 使用Power BI Desktop 制作并发布到Power BI 服务,使用Power BI Mobile查询报表
上节内容中,我们介绍了Power BI的基本概念,本节我们分享以下一个简单报表从使用Power BI Desktop制作,到发布到Power BI 服务,到从Power BI Mobile上查阅报表的 ...
- 在CentOS 7 上使用Docker 运行.NetCore项目
安装Docker CentOS 7 安装 Docker 编写Dockerfile 右键项目->添加->Docker 支持 选择Linux 修改为如下: FROM mcr.microsoft ...
- js如何操作sass里的变量及calc 使用sass变量
scss文件里 :root { --height-primary: 240px; //--height-primary :变量名,css3有规则 } $header: var(--height-pri ...
- vue组件初始化过程
之前文章有写到vue构造函数的实例化过程,只是对vue实例做了个粗略的描述,并没有说明vue组件实例化的过程.本文主要对vue组件的实例化过程做一些简要的描述. 组件的实例化与vue构造函数的实例化, ...
- eth-trunk学习笔记
转载自https://blog.csdn.net/qq_38265137/article/details/80404340
- Java编译时常量和运行时常量
Java编译时常量和运行时常量 编译期常量指的就是程序在编译时就能确定这个常量的具体值. 非编译期常量就是程序在运行时才能确定常量的值,因此也称为运行时常量. 在Java中,编译期常量指的是用fina ...
- EggJS接口开发
需求 随着Nodejs的普及,前端开发的开发场景基本可以贯穿界面交互到数据存储,无缝实现全栈开发.最近在实现一个内部项目管理工具的时候,就尝试了一把接口和数据库开发. 什么是Egg.js Egg.js ...