[抄题]:

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);
}
}

865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树的更多相关文章

  1. [LeetCode] 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 n ...

  2. LeetCode 865. Smallest Subtree with all the Deepest Nodes

    原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...

  3. 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [Swift]LeetCode865. 具有所有最深结点的最小子树 | 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 n ...

  5. leetcode_865. Smallest Subtree with all the Deepest Nodes

    https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树 ...

  6. FB面经 Prepare: LCA of Deepest Nodes in Binary Tree

    给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. FormData上传文件(不是所有的浏览器都支持)

    <h1>Ajax上传文件</h1> <div> <input type="file" id="img"/> &l ...

  2. jianx vtritualbox 虚拟镜像的体积

    https://blog.csdn.net/ganshuyu/article/details/46360271

  3. vue.js 初级之一

    vue.js 是一个构建数据驱动的 web 界面 渐进式驱动框架. 引用的话,直接使用script标签引入就可以了: <script src="./lib/vue.js"&g ...

  4. 跨域(六)——window.name

    window.name也可以进行跨域数据传输. 下面是相应的代码,evil.html跨域读取foo.html的数据,其中proxy.html和evil.html同域,没有任何内容. evil.html ...

  5. JS代码注释

    1.css和js都可以使用/**/进行注释 2.html使用<!---->注释 3.单行js代码可以使用//进行注释 <!DOCTYPE html> <html lang ...

  6. 创建DLL动态链接库——模块定义法(def)

    DLL模块定义法(Module-Definition File,即DEF):在VS家族IDE中,根据提示新增.def文件,如下: LIBRARY 关键字; mytestDll 库名; DLL_ADD ...

  7. C++ 关于MFC List Control 控件的使用事项 原创

    1\在开发项目时,使用到了 listcontrol 控件,就一些问题,做一下备注,以备以后使用 (1)  给list项目 删除所有的项目  DeleteAllItems(); (2) 给list项目 ...

  8. C# 保证数据长度相同

    /// <summary> /// 保证数据长度相同 /// </summary> /// <param name="obj"></par ...

  9. 吴裕雄 15-MySQL LIKE 子句

    我们知道在 MySQL 中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录.WHERE 子句中可以使用等号 = 来设定获取数据的 ...

  10. ADO.Net创建数据模型和数据访问类及泛型集合

    数据模型和数据访问类:数据模型: 使用面向对象中的封装特性,将数据表中的行数据组成一个同样结构的对象,来单独使用: 数据访问类: 将某一个表的全部增删改查操作的方法写进去,方便统一管理和调用: 数据模 ...