zoukankan      html  css  js  c++  java
  • [LeetCode] 652. Find Duplicate Subtrees

    Given the root of 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 the same node values.

    Example 1:

    Input: root = [1,2,3,4,null,2,4,null,null,4]
    Output: [[2,4],[4]]
    

    Example 2:

    Input: root = [2,1,1]
    Output: [[1]]
    

    Example 3:

    Input: root = [2,2,2,3,null,3,null]
    Output: [[2,3],[3]]

    Constraints:

    • The number of the nodes in the tree will be in the range [1, 10^4]
    • -200 <= Node.val <= 200

    寻找重复的子树。请返回一个list,里面包含的是所有重复的子树的根节点。对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。两棵树重复是指它们具有相同的结构以及相同的结点值。

    思路是后序遍历 + 序列化。题眼是子树,那么必然是后序遍历。因为只有先看更小的树是否相同,你才能在把某个子树往它的父节点传的时候发现一棵相同的,且更大的树。

    但是如何找相同的子树呢,我们应该是需要一个类似hashmap的东西存放子树,这样才能再以后再看到类似的树的结构的时候。这里我们运用到了之前297题的思路,把树的信息序列化。这样我们才能把这个信息当做key存到一个hashmap里。当我们第一次存的时候,就直接存;当我们再次遇到这个相同的key的时候,此时这个key背后的value是1,我们则知道出现了重复的子树,我们则可以把这个子树的根节点加到结果集里面。

    时间O(n)

    空间O(n)

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode() {}
     8  *     TreeNode(int val) { this.val = val; }
     9  *     TreeNode(int val, TreeNode left, TreeNode right) {
    10  *         this.val = val;
    11  *         this.left = left;
    12  *         this.right = right;
    13  *     }
    14  * }
    15  */
    16 class Solution {
    17     public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
    18         List<TreeNode> res = new ArrayList<>();
    19         // corner case
    20         if (root == null) {
    21             return res;
    22         }
    23         
    24         // normal case
    25         helper(root, res, new HashMap<>());
    26         return res;
    27     }
    28     
    29     private String helper(TreeNode cur, List<TreeNode> res, HashMap<String, Integer> map) {
    30         if (cur == null) {
    31             return "#";
    32         }
    33         String serial = cur.val + "," + helper(cur.left, res, map) + helper(cur.right, res, map);
    34         if (map.getOrDefault(serial, 0) == 1) {
    35             res.add(cur);
    36         }
    37         map.put(serial, map.getOrDefault(serial, 0) + 1);
    38         return serial;
    39     }
    40 }

    相关题目

    297. Serialize and Deserialize Binary Tree

    449. Serialize and Deserialize BST

    536. Construct Binary Tree from String

    606. Construct String from Binary Tree

    LeetCode 题目总结

  • 相关阅读:
    StackOverFlow 最有影响力的IT书箱 [Share]
    Principles of Distributed Computing [Share]
    C/C++ 命令行参数
    函数指针
    coursera-北大-计算概论【已完成】
    有关宏定义一题
    blog.163.com 崩溃以后~
    一步步学习SPD2010--第十一章节--处理母版页
    一步步学习SPD2010--第十章节--SP网站品牌化(12)--关键点
    一步步学习SPD2010--第十章节--SP网站品牌化(11)--使用CSS报表
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13840562.html
Copyright © 2011-2022 走看看