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. C#窗体的resx文件

    这些图片在项目文件中没找到,原来都存在了resx文件中. 属性界面的Image.BackgroundImage属性手动选择的图片会自动存储到resx文件中,之后这些图片源文件就可以删除了.resx中的 ...

  2. 【Python网络】网络协议篇

    OSI七层协议 互联网协议按照功能不同分为OSI七层 或 TCP/IP五层 或 TCP/IP四层 每层运行常见物理设备 TCP/IP五层模型讲解 每层都运行特定的协议,越往上越靠近用户,越往下越靠近硬 ...

  3. docker启动、关闭、重启命令

    docker启动命令,docker重启命令,docker关闭命令 启动        systemctl start docker守护进程重启   sudo systemctl daemon-relo ...

  4. HDU 5794 A Simple Chess ——(Lucas + 容斥)

    网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...

  5. HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)

    参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...

  6. VMWare workstation12配置CentOS6.5虚拟机NAT网络以及虚拟机静态IP

    1.右键网络连接—>打开网络和共享中心—>更改适配器设置—>找到网络适配器VMware Virtual Ethernet Adapter for VMnet8—>如下图所示修改 ...

  7. shell编程-定时删除(30天)文件

    1.创建shell touch /opt/auto-del-30-days-ago.sh chmod +x auto-del-30-days-ago.sh 2.编辑shell脚本: vi auto-d ...

  8. 【Nginx】Linux 环境下 Nginx 配置SSL 证书

    一.解压三个包到相同目录编译nginx cd /usr/local/src/nginx-1.12.2 # 将下列的包版本号换成自己下载的版本号 ./configure --prefix=/usr/lo ...

  9. stingstream 类

    使用完后在使用必须要clear():

  10. python:科学计数法转化为浮点型数据

    def as_num(x): y='{:.5f}'.format(x) # 5f表示保留5位小数点的float型 return(y) 实验一下 as_num(1.2e-4) In [3]:as_num ...