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. DBProxy 读写分离使用说明

    美团点评DBProxy读写分离使用说明   目的 因为业务架构上需要实现读写分离,刚好前段时间美团点评开源了在360Atlas基础上开发的读写分离中间件DBProxy,关于其介绍在官方文档已经有很详细 ...

  2. Java:下拉列表绑定后台数据

    后台传进来一个List集合,存着某对象集合,将其显示在下拉列表 一.HTML代码 页面有个下拉列表,如图所示: <td style="width:30%"> <s ...

  3. c# 十进制转二、八、十六进制

    一.十进制转二.八.十.十六进制字符串 Convert.ToString(int decNum,int toBase); decNum为十进制字符串, toBase可以为2.8.10.16 如果要转换 ...

  4. 深入理解java虚拟机---java虚拟机内存管理(七)

    本地方法栈.java堆.方法区 本地方法栈在HotSpot版本内与java虚拟机栈是合二为一的.不单独区分本地方法栈.但是java虚拟机中是有这样一块区域的. 作用: 1.本地方法栈为虚拟机栈执行ja ...

  5. Problem E: 编写函数:Swap (I) (Append Code)

    Description 编写用来交换两个数的函数,使得“Append Code”中的main()函数能正确运行. ------------------------------------------- ...

  6. 给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数。

    题目:给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数. 解题思路: 首先取得数组a的中位数a[aMid],然后在b中二分查找a[aMid],得到b[bMid],b[bSt] ...

  7. 关于索引的相关 day45

    mysql数据库索引相关   一 介绍 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能 ...

  8. Vue 项目架构设计与工程化实践

    来源 文中会讲述我从0~1搭建一个前后端分离的vue项目详细过程 Feature: 一套很实用的架构设计 通过 cli 工具生成新项目 通过 cli 工具初始化配置文件 编译源码与自动上传CDN Mo ...

  9. 一种基于SDR实现的被动GSM嗅探

    软件定义无线电(SDR)是一种无线电通信系统,简单来说,就是通过数字信号处理技术在通用可编程数字信号处理硬件平台上,利用软件定义来实现无线电台的各单元功能,从而对无线电信号进行调制.解调.测量.SDR ...

  10. git 实现提交远程分支步骤

    git clone git branch [分支名] 创建分支 git branch 查看本地所有分支 git checkout [分支名称] 切换分支 ---写代码--- git status (查 ...