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
  • 相关阅读:
    ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现
    WEB API 中HTTP的get、post、put,delete 请求方式
    SQLServer中的事务与锁
    C#迭代器
    C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行
    SD卡WAV音乐播放器(quartus11.0)(FAT32)(DE2-115)
    Xilinx IP核的根目录地址,有datasheet 和仿真相关的资料
    quartusii 使用ModelSim do文件实现仿真(Verilog)
    怎样用modelsim做后仿真
    ModelSim之tcl自动化仿真
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9808326.html
Copyright © 2011-2022 走看看