核心思想是:序列化树

序列化后,用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找到重复树的更多相关文章

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

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

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

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

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

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

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

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

  6. LC 652. Find Duplicate Subtrees

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

  7. LeetCode 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  9. LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

    题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Jav ...

随机推荐

  1. mysql给用户赋予所有权限

    mysql给用户赋予所有权限(包括远程连接) 我们给mysql新创建的用户,希望它拥有更多权限,比如远程连接,方便我们操作,可以使用如下命令: GRANT ALL PRIVILEGES ON *.* ...

  2. MySQL错误日志(Error Log)

    错误日志是一个文本文件,记录了 MySQL Server 每次启动和关闭的详细信息以及运行过程中所有较为严重的警告和错误信息.在遇到问题时,应该首先查看这个文件. 如何开启 使用命令 SHOW VAR ...

  3. Codeforces Round 665 (div2)

    2020.8.22 装修完了我的博客,喜欢这个造型,挂上友链就更好了 昨天cf就是一个彻头彻尾的悲剧,本来能上蓝,结果因为在A题耽误时间过多导致掉了30分,不过没关系,这算是一个小波动吧,影响不了什么 ...

  4. 第二十章、QTableView与QStandardItemModel开发实战:展示Excel文件内容

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在前面<第十九章.Model/View开发:QTableView的功能及属性> ...

  5. PyQt(Python+Qt)学习随笔:model/view架构中QTableView视图的数据无法显示问题

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在使用QTableView来显示如下数据时: 在model中插入数据是使用如下类似代码: for c ...

  6. PyQt(Python+Qt)学习随笔:QListView的layoutMode属性和batchSize属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 batchSize属性 该属性是在layoutMode属性设置为Batched时,用于控制每个批量的 ...

  7. PyQt学习随笔:自定义Qt事件可以使用的事件类型的常量值范围

    除了<PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料速查>介绍的Qt已经定义的事件外,Qt还支持自定义事件. 为了方便起见,可以使用 registerEventTyp ...

  8. BUUOJ WEB(1)

    [ACTF2020 新生赛]Include 开启环境之后点击tips 可以在url中看到格式为: ?file=flag.php 加上题目是include,可以猜测是文件包含漏洞 http://a291 ...

  9. Codeforces Round #682 Div2 简要题解

    Contest link A.Specific Tastes of Andre Problem link 题意 构造一个长度为 \(n\) 的序列,使得每个非空子序列的和都被其长度整除. 思路 直接每 ...

  10. mybatis逆向工程运行

    命令: mvn mybatis-generator:generate 项目结构: generatorConfig.xml内容示例 <?xml version="1.0" en ...