zoukankan      html  css  js  c++  java
  • 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树

    [抄题]:

    Given a binary tree rooted at root, the depth of each node is the shortest distance to the root.

    A node is deepest if it has the largest depth possible among any node in the entire tree.

    The subtree of a node is that node, plus the set of all descendants of that node.

    Return the node with the largest depth such that it contains all the deepest nodes in its subtree.

     

    Example 1:

    Input: [3,5,1,6,2,0,8,null,null,7,4]
    Output: [2,7,4]
    Explanation:
    
    
    
    We return the node with value 2, colored in yellow in the diagram.
    The nodes colored in blue are the deepest nodes of the tree.
    The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
    The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
    Both the input and output have TreeNode type.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    毫无思路,看了一眼后觉得终于会写一点recursion了

    [思维问题]:

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

    [一句话思路]:

    depth的recursion存储最长路径

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

    [画图]:

    [一刷]:

    1. maxdepth 不是left +right+1,而是二者中的较大值加1
    2. dfs中有left = right的情况

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

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

    [总结]:

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

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

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

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

     [代码风格] :

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

     [潜台词] :

    还是有BUG,不知道为啥

    class Solution {
      //get depth
        public int depth(TreeNode root, HashMap<TreeNode, Integer> map) {
          //corner case
          if (root == null) return 0;
          
          //if exist, return
          if (map.containsKey(root)) 
            return map.get(root);
          
          
          //or put into map
          int max = Math.max(depth(root.left, map), depth(root.right, map)) + 1;
          map.put(root, max);
          
          return max;
        }
      //do dfs
      public TreeNode dfs(TreeNode root, HashMap<TreeNode, Integer> map) {
        //corner case
        //if (root == null) return null;
        
        //dfs in left or in right
        int left = depth(root.left, map);
        int right = depth(root.right, map);
        if (left == right) return root;
        if (left > right) dfs(root.left, map);
        return dfs(root.right, map);
      }
      
        public TreeNode subtreeWithAllDeepest(TreeNode root) {
            //remember corner case
            if( root == null ) return null;
            //initialization
          HashMap<TreeNode, Integer> map = new HashMap<TreeNode, Integer>();
          return dfs(root, map);
        }
    }
    View Code
  • 相关阅读:
    [置顶] 一个懦弱的IT人
    Android ListView的理解(一)
    不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数
    http-使用get和post方式提交数据
    ILOG的一个基本应用——解决运输问题、转运问题
    原生js获取execl里面的值 主要使用ActiveXObject
    (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
    HDU 3032 Nim or not Nim? (sg函数)
    Hadoop入门实践之从WordCount程序说起
    仅复制备份(简单恢复模式)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9808326.html
Copyright © 2011-2022 走看看