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 listn.

分析:把每个node当成root,然后得到它的preorder traversal string, 因为子node的preorder traversal string是可以被用在parent上的,这样时间复杂度只是O(n).

 class Solution {
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> res = new LinkedList<>();
preorder(root, new HashMap<>(), res);
return res;
} public String preorder(TreeNode cur, Map<String, Integer> map, List<TreeNode> res) {
if (cur == null) return "#";
String serial = cur.val + "," + preorder(cur.left, map, res) + "," + preorder(cur.right, map, res);
if (map.getOrDefault(serial, ) == ) res.add(cur);
map.put(serial, map.getOrDefault(serial, ) + );
return serial;
}
}

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. [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees

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

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

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

  4. LeetCode - Find Duplicate Subtrees

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

  5. LeetCode——Find Duplicate Subtrees

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

  6. [leetcode-652-Find Duplicate Subtrees]

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

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

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

  8. LC 652. Find Duplicate Subtrees

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

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

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

随机推荐

  1. ListView如何添加数据如何不闪烁

    public class DoubleBufferListView : ListView        {            public DoubleBufferListView()       ...

  2. RestFul是啥

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...

  3. Object.keys 返回由一个给定对象的自身可枚举属性组成的数组

    在实际开发中,我们有时需要知道对象的所有属性, 原生js给我们提供了一个很好的方法:Object.keys(),该方法返回一个数组 http://blog.csdn.net/u014035151/ar ...

  4. jQuery事件之绑定事件

    语法: $(selector).bind(eventType[, eventData], handler(eventObject)); 参数解释: eventType(String): 一个包含一个或 ...

  5. Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)

    题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...

  6. AcWing:164. 可达性统计(拓扑排序 + 状态压缩算法)

    给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条有向边. 输出格式 输出共N行,表示每个点能 ...

  7. Nmap简单的漏扫

    转载至 https://www.4hou.com/technology/10481.html   导语:Nmap本身内置有丰富的NSE脚本,可以非常方便的利用起来,当然也可以使用定制化的脚本完成个人的 ...

  8. 关于开发APP接口版本不兼容的问题

    关于 APP接口版本兼容的问题. iOS和android 要不断开发新版本,很多服务端开发都是在以前接口的逻辑上进行修改. 新的APP和接口开发后,接口如何兼容老的APP? 有的公司 每次发布完APP ...

  9. ai笔记

  10. LeetCode 77. 组合(Combinations)

    题目描述 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...