Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof 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.

思路:

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

string serialize(TreeNode* root,unordered_map<string,vector<TreeNode*> >& mp)
{
if(root==nullptr) return "";
string s= "(" + serialize(root->left,mp) + to_string(root->val) + serialize(root->right,mp) + ")";
mp[s].push_back(root);
return s;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root)
{
unordered_map<string,vector<TreeNode*> >mp;
vector<TreeNode*>ret;
serialize(root,mp);
for(auto it = mp.begin();it != mp.end();it++)
{
if(it->second.size() > ) ret.push_back(it->second[]);
}
return ret;
}

参考:

https://discuss.leetcode.com/topic/97601/c-clean-code

[leetcode-652-Find Duplicate Subtrees]的更多相关文章

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

    核心思想是:序列化树 序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示): 一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加. 重点就是 ...

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

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

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

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

  4. LC 652. Find Duplicate Subtrees

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

  5. 652. Find Duplicate Subtrees

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

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

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

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

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

  8. LeetCode - Find Duplicate Subtrees

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

  9. LeetCode——Find Duplicate Subtrees

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

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

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

随机推荐

  1. 你不知道的javaScript笔记(3)

    对象 对象可以通过两种形式定义: 声明形式和构造形式 声明形式语法: var myObj = {key:value} 构造形式语法: var myObj = new Object(); myObj.k ...

  2. ABAP术语-IDOC

    IDOC 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/21/1075988.html Intermediate Document Inte ...

  3. ABAP术语-Distribution Model

    Distribution Model 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/25/1052434.html Model that d ...

  4. 单机安装hadoop+hive+presto

    系统环境 在个人笔记本上使用virtualbox虚拟机 os:centos -7.x86-64.everything.1611  ,内核 3.10.0-514.el7.x86_64 注:同样可以使用r ...

  5. datatable根据条件设置表格行中字的颜色或背景

    使用row回调函数 "rowCallback": function (row, data) { if (xxx) { //给行添加背景色 $(row).css("back ...

  6. (转)Maven POM中的各种scope的行为总结

    原地址:https://blog.csdn.net/cnweike/article/details/52221410 compile:默认的scope.任何定义在compile scope下的依赖将会 ...

  7. Struts2拦截器说明

    有关于Struts2的拦截器的原理 在此共设置了两个拦截器,firstInterception.SecondInterception package struts2_inteception; publ ...

  8. Mit6.824 Lab1-MapReduce

    前言 Mit6.824 是我在学习一些分布式系统方面的知识的时候偶然看到的,然后就开始尝试跟课.不得不说,国外的课程难度是真的大,一周的时间居然要学一门 Go 语言,然后还要读论文,进而做MapRed ...

  9. github 常用

    1.创建KEY,这个文件生成完了后,要保存好公钥和私钥文件 ssh-keygen -t rsa -C "abc@mail.com" 2.github上添加ssh密钥 3.拷贝公钥信 ...

  10. JDK6 新特性

    JDK6新特性目录导航: Desktop类和SystemTray类 JAXB2实现对象与XML之间的映射 StAX Compiler API 轻量级 Http Server API 插入式注解处理AP ...