核心思想是:序列化树

序列化后,用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. Java lambda 分组后多列求和

    主要思路是reducing,可以像sql一样分组后多列求和处理成新对象等: select code,max(name)as name,sum(chengJi)as chengJi,sum(age)as ...

  2. 「考试」noip模拟9,11,13

    9.1 辣鸡 可以把答案分成 每个矩形内部连线 和 矩形之间的连线 两部分 前半部分即为\(2(w-1)(h-1)\),后半部分可以模拟求(就是讨论四种相邻的情况) 如果\(n^2\)选择暴力模拟是有 ...

  3. Linux之【GNU】、【GPL】、【linux系统组成】

    GNU,什么是GNU GNU全称:GNU's not unix GNU的重要组件(Emacs,gcc,bash,gawk等)加上自己的内核构成了GNU自己的系统--->没用 现在linux中的一 ...

  4. 第15.32节 PyQt(Python+Qt)入门学习:containers容器类部件QToolBox工具箱介绍及使用案例

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 容器部件就是可以在部件内放置其他部件的部件,在Qt Designer中可以使用的容器部件有 ...

  5. Oracle批量新增数据最佳实践

    一.需求描述 现在的项目改造过程中,从国产的Gbase数据库改造为Oracle数据库,遇到一个问题有的业务操作需要批量新增数据. 这也是一个比较常规的操作,有很多地方确实需要一次性新增多条数据.Gba ...

  6. 利用promise实现间隔1s打印1,2,3

    利用promise结合数组的rduce方法 let arr = [1, 2, 3]; arr.reduce((pre, cur) => { return pre.then(() => { ...

  7. Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 D题题解

    将题意转换为一开始\(t = 0\),第\(i\)个操作是令\(t \leftarrow (a_i + 1) t + (a_i + b_i + 1)\).记\(A_i = a_i + 1, B_i = ...

  8. ajax与浏览器请求的差异对比.png

  9. 廖雪峰官网学习js 数组

    indexOf( )    某字符的位置 slice 相当于string 的substring 切片 a = ['a','b',1,2,3] (5) ["a", "b&q ...

  10. linux下postgresql安装

    1.下载地址 https://www.enterprisedb.com/download-postgresql-binaries 2.创建用户并修改密码 [root@node01 ~]# userad ...