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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/find-duplicate-subtrees/description/

题目描述:

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.

题目大意

找出一个二叉树中所有的重复子树。重复子树就是一棵子树,在大的二叉树中出现的次数不止一次。

解题方法

暴力解法不可取。

参考了[LeetCode] Find Duplicate Subtrees 寻找重复树才明白,我们要找到一个重复的子树,可以把树和hash结合起来啊!

每一棵子树,都能把它的结构使用先序遍历或者后序遍历保存下来,然后把这个结构保存在hash表格里面,这样当我们下次再遇到这个树的结构的时候,就能很容易查表得知,然后放到结果res中。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findDuplicateSubtrees(self, root):
"""
:type root: TreeNode
:rtype: List[TreeNode]
"""
res = []
m = collections.defaultdict(int)
self.helper(root, m, res)
return res def helper(self, root, m, res):
if not root:
return '#'
path = str(root.val) + ',' + self.helper(root.left, m, res) + ',' + self.helper(root.right, m, res)
if m[path] == 1:
res.append(root)
m[path] += 1
return path

日期

2018 年 7 月 15 日 ———— 昨天火锅+啤酒,今天要加班了。。

【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

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

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

  3. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  4. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. Linux命令行好玩的命令

    0.cal 2019  #输出日历并显示今天是哪一天 1.命令"date",显示系统的当前日期和时间: 2.命令"date 040100002016",屏幕显示 ...

  2. 在前端页面中使用Markdown并且优化a标签

    近期在自己的项目中加入了对 Markdown 语法 的支持,主要用到的是markedjs这个项目.该项目托管在github上,地址为:https://github.com/markedjs/marke ...

  3. Z可读作zed的出处?

    Commercial and international telephone and radiotelephone SPELLING ALPHABETS between World War I and ...

  4. 同步阻塞IO模型

    同步阻塞IO模型 有上篇IO模型中的,同步阻塞IO模型,我们能够知道,用户线程发起请求后就一直阻塞的等待 内核完成准备数据.数据拷贝的工作.并且返回成功的指示. 实现 使用java来实现同步阻塞IO模 ...

  5. [PE结构]导出表结构浅析

    导出函数的总数-->以导出函数序号最大的减最小的+1,但导出函数序号是可自定义的,所以NumbersOfFunctions是不准确的 1.根据函数名称找,函数名称表->对应索引函数序号表中 ...

  6. 如何在Swagger2或Swagger3中增加Json Web Token

    1. 前言 Swagger 3.0已经发布有一段时间了,作为一个非常有用的文档工具已经越来越多的项目在使用它.而JWT也是目前前后端分离最常用的安全技术.那么如何在Swagger 3.0 中添加JWT ...

  7. [学习总结]4、Android的ViewGroup中事件的传递机制(一)

    本文主要针对dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent三个方法,通过简单的例子来简单的介绍下. 根据字面意思的理解,dispatchTo ...

  8. Layui:select下拉框回显

    一..需求场景分析 基于Thymeleaf模板下的layui下选框回显. 二.获得一个Layui标配的下拉框,我们需要在html中填写的内容如下 <div class="layui-f ...

  9. CURD系统怎么做出技术含量惊艳面试官

    在<CURD系统怎么做出技术含量--怎样引导面试>有朋友开玩笑说都用上了领域驱动了,就不叫CURD系统了吧.这里我解释一下,怕大家对DDD领域驱动设计有什么误解. DDD是为解决软件复杂性 ...

  10. Gitlab-CICD实践篇

    一.背景 随着公司项目使用gitlab越来越多,业务发布的次数越来越频繁,对于发布效率提出了更高的要求.从2012开始,Gitlab官方开始集成了Continuous Integration (CI) ...