333. Largest BST Subtree节点数最多的bst子树
[抄题]:
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
Note:
A subtree must include all of its descendants.
Example:
Input: [10,5,15,1,8,null,7] 10
/ \
5 15
/ \ \
1 8 7 Output: 3
Explanation: The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
最后结果可能是计算方法正确的负数,还没转过来,所以要再转一次。
[思维问题]:
如果每个点检查valid时都还要traverse,就会形成n2的复杂度。
return isValid(root.left, min, root.val) && isValid(root.right, root.val, max);
所以定义一个新的类,只管自己不管别人。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
方法名和类名保持相同
[一句话思路]:
所以定义一个新的类,只管自己不管别人。因为算法没错,不对时直接*-1转过来就行。
Your input
[10,5,15,1,8,null,7]
Your stdout
left.res = 0
right.res = 1
root.val = 15
left.max = -2147483648
right.min = 7
Math.max(Math.abs(left.res), Math.abs(right.res)) = 1 left.res = 3
right.res = -1
root.val = 10
left.max = 8
right.min = 0
Math.max(Math.abs(left.res), Math.abs(right.res)) = 3 Your answer
3
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
定义一个新的类,只管自己不管别人。因为算法没错,不对时直接*-1转过来就行。
[复杂度]: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 {
class Result {
int res;
int min;
int max; public Result(int res, int min, int max) {
this.res = res;
this.min = min;
this.max = max;
}
} public int largestBSTSubtree(TreeNode root) {
Result result = helper(root);
return Math.abs(result.res);
} //divide and conquer, reverse or add to a new num
public Result helper(TreeNode root) {
//corner case
if (root == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE); //form left and right
Result left = helper(root.left);
Result right = helper(root.right); //reverse: root's val incorrect or res < 0
if (root.val < left.max || root.val > right.min || left.res < 0 || right.res < 0) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.res)) * (-1), 0, 0);
}else {
return new Result(1 + left.res + right.res, Math.min(left.min, root.val), Math.max(right.max, root.val));
}
}
}
333. Largest BST Subtree节点数最多的bst子树的更多相关文章
- [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- 【LeetCode】333. Largest BST Subtree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 333. Largest BST Subtree
nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Locked] Largest BST Subtree
Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...
随机推荐
- lvs UDP端口负载均衡配置
! Configuration File for keepalived global_defs { notification_email { test@163.com } notification_e ...
- ILBC 运行时 (ILBC Runtime) 架构
本文是 VMBC / D# 项目 的 系列文章, 有关 VMBC / D# , 见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>) https:// ...
- centos-rpm安装的mariadb,php52源码编译安装时注意点
1.不要静态指定with-mysql 以扩展的mysql.so的形式安装 2.找不到header file之类的 要yum install mysql-devel find / -name mysql ...
- ubuntu-docker入门到放弃(八)创建支持SSH服务的镜像
我们知道进入docker容器可以使用attach.exec等命令来操作和管理,但是如果需要远程登录并管理容器,就需要ssh服务的支持了. 1.基于commit命令创建 docker提供了commit命 ...
- ubuntu server资料
2.改变键盘布局 sudo dpkg-reconfigure keyboard-configuration 或sudo vim /etc/default/keyboard,修改XKBLAYOUT变量的 ...
- Mysql临时文件目录控制
查看mysql的log-error日志发现如下错误: ERROR 3 (HY000): Error writing file '/tmp/MYbEd05t' (Errcode: 28) 这是由于mys ...
- selenium +chromdriver模块
1 selenium 模拟浏览器行为 2 chromdriver 对应的chrome浏览器驱动 下载地址 注意:chrome与chromdriver存在对应关系 以下错误就可能是版本不对应 ...
- minicom 抓取log
使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...
- .net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.)
https://cn.mathworks.com/matlabcentral/answers/278399-i-get-an-error-saying-the-type-initializer-for ...
- Open SuSE中自定义的环境变量
针对与其它发行版本的Linux,网络上给出的添加环境变量的位置都是在/etc/profile文件中添加.在Open SuSE中也有/etc/profile文件,不过从该文件的前几行注释可以看出,官方建 ...