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.

Example 1:

        1
/ \
2 3
/ / \
4 2 4
/
4
The following are two duplicate subtrees: 2
/
4
and 4
Therefore, you need to return above trees' root in the form of a list.

这道题考的是DFS+序列化二叉树

我们将每一个节点的左子节点的值和右结点的值都存储下来,组成一个字符串,作为索引,将对应节点保存到map里。

如果一样的字符串已经出现过一次了,我们就把他的root保存在要返回的list中:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution { Map<String, Integer> map;
List<TreeNode> list; public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
this.map = new HashMap<>();
this.list = new ArrayList<>();
collect(root);
return list;
}
private String collect (TreeNode node ){
if(node == null){return "#";}
String serial = node.val +","+collect(node.left) +","+collect(node.right);
map.put(serial, map.getOrDefault(serial,0)+1);
if(map.get(serial) ==2 ){
list.add(node);
}
return serial;
}
}

  

LeetCode - 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——Find Duplicate Subtrees

    Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...

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

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

  4. LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees

    LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...

  5. [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees

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

  6. [leetcode-652-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. LC 652. Find Duplicate Subtrees

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

  9. Find Duplicate Subtrees

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

随机推荐

  1. loj6277

    题解: 树状数组模板提 代码: #include<bits/stdc++.h> using namespace std; ; int num[N],n,a[N],l,r,c,opt; vo ...

  2. CSS(一)属性--border边框

    HTML代码 <body> <div>举个例子</div> </body> CSS代码: div{ font-size:12px;  //字体大小,默认 ...

  3. .net core webapi 配置swagger调试界面

    一.创建一个.net core webapi的项目: 二.在nuget程序包管理器控制台输入  Install-Package Swashbuckle -version 6.0.0-beta902   ...

  4. EF-一对一关系

    针对关系型数据库来说,需要明了每个对象之间的关系. 它们之间的关系有: 1.一对一(1:1):一个学生只能拥有一张身份证,一张身份证只能属于一个学生: 2.一对多(1:N):一个学生可以拥有几本书,而 ...

  5. 红黑树( Red-Black Tree ) - 笔记

    1.  红黑树属性:根到叶子的路径中,最长路径不大于最短路径的两倍. 2. 红黑树是一个二叉搜索树,并且有 a. 每个节点除了有左.右.父节点的属性外,还有颜色属性,红色或者黑色. b. ( 根属性  ...

  6. Zabbix4.0监控URL

    一:新建群组 1.1:web monitor 二:新建模板 2.1:配置-模板-模板 2.3:创建应用集 配置-模板-web monitor-应用集 2.4:创建web场景 2.5:创建场景步骤: 以 ...

  7. 第六节 静态的(static)和单例模式

    main函数 主函数是一个特殊的函数,作为程序的入口,可以被jvm(虚拟器)调用 主函数的定义 public 表示该函数的访问权限是最大的. static 代表主函数随着类的加载就已经存在了. voi ...

  8. 对称加密-java实现

    主要步骤如下: 1.利用SecretKeyFactory.getInstance("加密算法")创建密钥工厂,加密算法如"DES","AES" ...

  9. angular2组件通讯的几种方式

    最近刚刚接触angular2,对ng2也是一知半解,如有说得不对的地方欢迎指出,欢迎加q共同探讨学习991085978: 1.通过输入型绑定把数据从父组件传到子组件 HeroChildComponen ...

  10. L253 Valentine's Day

    Are you ready for Valentine's Day, my fellow stargazers? Not sure if you know this, but the astrolog ...