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存储最长路径
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- maxdepth 不是left +right+1,而是二者中的较大值加1
- 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 有最深节点的最小子树的更多相关文章
- [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 ...
- LeetCode 865. Smallest Subtree with all the Deepest Nodes
原题链接在这里:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 题目: Given a binar ...
- 【LeetCode】865. Smallest Subtree with all the Deepest Nodes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- leetcode_865. Smallest Subtree with all the Deepest Nodes
https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树 ...
- FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- HTTP梳理
HTTP请求头 Host:初始URL中的主机名和端口 Accept:浏览器可接受的MIME类型 Acceept-Charset:浏览器接受的字符集 Accept-Encoding:浏览器能够进行解码的 ...
- python 叠加装饰器详解
def out1(func1): #7.func1=in2的内存地址,就是in2 print('out1') def in1(): #8.调用函数index() 因为函数在in1里,所以首先运行in1 ...
- OGNL表达式(转载)
OGNL表达式(转载) 1.什么是OGNL OGNL:Object Graphic Navigation Language(对象图导航语言) 它是Struts2中默认的表达式语言.使用表达式需要借 ...
- shell编程小技巧(命令篇)
本文主要介绍shell编程中一些好用的命令或者一些常见命令但比较少用却又好用的参数,目的是希望可以提高编码效率. df命令 常用命令 df / df -k / df -m / df -H / df - ...
- Android DevArt6:Android中IPC的六种方式
Android中IPC的六种方式 1.使用Bundle 最简单的进程间通信方式:Intent + Bundle: 支持三大组件:Activity.Service.BroadcastReceiver : ...
- ANg-别人家的笔记
http://blog.csdn.net/scruelt/article/details/78997697 https://github.com/fengdu78/Coursera-ML-Andrew ...
- asp.net中的reportview报错跟预编有关系
当报表控件出现: 报表定义无效.详细信息:根级别上的数据无效.行1,位置1. 先检查一下,你的aspx文件是不是变成了这样一句话 这是预编译工具生成的标记文件,不应被删除! 如果这样的话,报表控件是不 ...
- C++ CSTRINGLIST用法
CStringList类成员 构造 CStringList 构造一个空的CString对象列表 首/尾访问 GetHead 返回此列表(不能是空的)中头部的元素 GetTail 返回此列表(不能是 ...
- C#中属性和字段的区别
属性和字段的区别 在C#中,我们可以非常自由的.毫无限制的访问公有字段,但在一些场合中,我们可能希望限制只能给字段赋于某个范围的值.或是要求字段只能读或只能写,或是在改变字段时能改变对象的其他一些状态 ...
- JAVA 课堂测试
package ACC; /*信1705-2班 * 20173623 * 赵墨涵 */ public class Account { String accountID; String accountn ...
We return the node with value 2, colored in yellow in the diagram.