[LeetCode]652. Find Duplicate Subtrees找到重复树
核心思想是:序列化树
序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示);
一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加。
重点就是序列化树,序列化得到的String可以唯一的代表一棵树,这个思想很多题都用到了
并不是只是前序遍历就能唯一表示一棵树,加上结构信息就可以了。
Map<String,TreeNode> map = new HashMap<>();
List<TreeNode> res = new ArrayList<>();
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
serialize(root);
return res;
}
public String serialize(TreeNode root)
{
StringBuilder s = new StringBuilder();
if (root==null)
{
s.append("#");
s.append(",");
return new String(s);
}
s.append(root.val);
s.append(",");
s.append(serialize(root.left));
s.append(serialize(root.right));
String cur = new String(s);
if (map.containsKey(cur))
{
TreeNode t = map.get(cur);
if (!res.contains(t)) res.add(t);
}
else map.put(cur,root);
return cur;
}
[LeetCode]652. Find Duplicate Subtrees找到重复树的更多相关文章
- [LeetCode] Find Duplicate Subtrees 寻找重复树
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 652. Find Duplicate Subtrees找出重复的子树
[抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...
- LC 652. Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)
题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...
随机推荐
- 虚拟机下Ubuntu共享文件夹不能显示的一种解决方法
原文链接:https://blog.csdn.net/huyangzhilin/article/details/70666937
- Beta冲刺随笔——Day_Ten
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
- MySQL数据库入门学习
一. 前言 作为一名大二在校生,因为正在学习网页设计,考虑到后台问题,所以便自学了数据库,可能给大家总结的不是很全,但是一些必要的点肯定会讲到.现在市场上有很多图形化的数据库, 二. MySQL基础知 ...
- 网络最大流 Dinic算法
前言 看到网上好多都用的链式前向星,就我在用 \(vector\) 邻接表-- 定义 先来介绍一些相关的定义.(个人理解) 网络 一个网络是一张带权的有向图 \(G=(V,E)\) ,其中每任意一条边 ...
- 第7.21节 Python抽象类—register注册虚拟子类
上两节介绍了Python抽象类的真实子类的定义和使用,本节介绍另一种抽象类的实现方法:虚拟子类方法. 一. 相关概念 虚拟子类是将其他的不是从抽象基类派生的类"注册"到抽象基 ...
- 第8.24节 使用__subclasses__查看类的直接子类
在<第8.9节 Python类中内置的__bases__属性>中介绍了__bases__这个类的特殊变量可以查看类的直接父类,而__subclasses__() 方法的使用则与__base ...
- Python正则表达式处理中的匹配对象是什么?
老猿才开始学习正则表达式处理时,对于搜索返回的匹配对象这个名词不是很理解,因此在前阶段<第11.3节 Python正则表达式搜索支持函数search.match.fullmatch.findal ...
- 第14.12节 Python中使用BeautifulSoup解析http报文:使用select方法快速定位内容
一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>和<第14.11节 Python中使用BeautifulSo ...
- PyQt(Python+Qt)学习随笔:QTreeView树形视图的sortingEnabled属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的sortingEnabled属性用于控制视图中的数据是否启用按表头排序, ...
- MapReduce简单执行过程及Wordcount案例
MapReducer运行过程 以单词统计为案例. 假如现在文件中存在如下内容: aa bb aa cc dd aa 当然,这是小文件,如果文件大小较大时会将文件进行 "切片" ,此 ...