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


    [抄题]:

    就是出现了多次的子树,可以只包括一个点。

    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

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    signature里的括号表示新建对象。由于是已经建出来以后再调用,就不要括号了。

    [一句话思路]:

    形成serial字符串,添加cur节点即可

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    serieToCount.getOrDefault(serie, 0) hash表的装逼写法应该熟悉了

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    形成serial字符串,添加cur节点即可

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

     

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
            //initialization
            List<TreeNode> result = new ArrayList<TreeNode>();
            HashMap<String, Integer> serieToCount = new HashMap<String, Integer>();
            
            //corner case
            if (root == null) return result;
            
            //preorder,return
            preOrder(root, serieToCount, result);
            
            return result;
        }
        
        public String preOrder(TreeNode root, HashMap<String, Integer> serieToCount, 
                               List<TreeNode> result) {
            //corner case : exit if cur is null
            if (root == null) return "#";
            
            //form serie
            String serie = root.val + "," + preOrder(root.left, serieToCount, result) + "," + preOrder(root.right, serieToCount, result);
            
            //if count == 1, add to res. or add count
            if (serieToCount.getOrDefault(serie, 0) == 1) result.add(root);
            serieToCount.put(serie, serieToCount.getOrDefault(serie, 0) + 1);
            
          return serie;  
        }
    }
    View Code
  • 相关阅读:
    Redis序列化存储Java集合List等自定义类型
    Redis序列化存储Java集合List等自定义类型
    Unity 实现Log实时输出到屏幕或控制台上<二>
    Object-C,NumberDemo和StringDemo
    Object-C,NumberDemo和StringDemo
    Object-C,四则运算计算器
    Object-C,四则运算计算器
    HDU 3732 Ahui Writes Word
    HDU 1176 免费馅饼
    HDU 2571 命运
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9440850.html
Copyright © 2020-2023  润新知