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. 协议类接口 - I2C

    一.12c总线概述 I2C( Inter-Integrated Circuit,又称IIC)总线是一种串行总线,用 于连接微控制器及其外围设备,硬件图非常简单:一条串行数据线(SDA),一条串行时钟线 ...

  2. 快速排序_C语言_数组

    快速排序_C语言_数组 #include <stdio.h> void quickSort(int *, int, int); int searchPos(int *, int, int) ...

  3. shell的命令格式

    参考高峻峰 著 循序渐进Linux(第二版) command [options] [arguments] command:表示命令的名称 options:表示命令的选项 arguments:表示命令的 ...

  4. java常见面试问题.你一定会预见到。

    1判断一个char字符是不是数字:Character.isDigit(char).是数字返回true,反之返回false. 2字符串的toCharArray() 把字符串转换为字符数组.返回char[ ...

  5. Java处理中文乱码问题

    package servlet; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.ser ...

  6. C# WebClient 使用http免费代理

    static void Main(string[] args) { WebClient client = new WebClient(); client.Encoding = Encoding.Get ...

  7. 一、Linux知识体系结构图

    参考: https://blog.csdn.net/Swing_Liu/article/details/79202479

  8. pynlpir + pandas 文本分析

    pynlpir是中科院发布的一个分词系统,pandas(Python Data Analysis Library) 是python中一个常用的用来进行数据分析和统计的库,利用这两个库能够对中文文本数据 ...

  9. unity独立游戏开发日志2018/09/26

    最近太忙,今天吃饭的时候灵感一现...想到了随机地图生成的方法,不过可能实现的比较笨...还需要优化,大佬绕过. 注释没打,最后统一解释. using System.Collections; usin ...

  10. PHP教程专题资源免费下载地址收藏

     PHP教程专题资源免费下载地址收藏 PHP,即Hypertext Preprocessor,是一种被广泛应用的开源通用脚本语言,尤其适用于 Web 开发并可嵌入 HTML 中去.它的语法利用了 C. ...