[抄题]:

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子树的更多相关文章

  1. [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  3. [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  4. LeetCode 333. Largest BST Subtree

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

  5. 【LeetCode】333. Largest BST Subtree 解题报告(C++)

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

  6. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  7. 333. Largest BST Subtree

    nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...

  8. Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. [Locked] Largest BST Subtree

    Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...

随机推荐

  1. Django学习笔记之URL与视图

    视图 视图一般都写在app的views.py中.并且视图的第一个参数永远都是request(一个HttpRequest)对象.这个对象存储了这个http请求的所有信息,其中包括携带的参数以及一些头部信 ...

  2. Python Django orm操作数据库笔记之外键和表关系

    外键 在MySQL中,表有两种引擎,一种是InnoDB,另外一种是myisam.如果使用的是InnoDB引擎,是支持外键约束的. 外键的使用 使用外键前需要先确保相应外键已存储在数据库中(flask中 ...

  3. a small notepad++ plugin to support doxygen 1key generate

    Precondition: doxygen in c:\ folder testdoxygen.bat --- path %path%;C:\Doxygen;C:\Doxygen\graphviz\b ...

  4. Linux内核分析第二次作业

    这周学习了<庖丁解牛Linux内核分析>并且学习了实验楼的相关知识. 在实验楼的虚拟环境下编写代码: 通过gcc编译后,使用查看文件命令:cat  -n 20189223.c 在vim中, ...

  5. asp.net:mv4 FileResult在IE8中下载不显示文件名和扩展名而显示Action方法名了!

    IE8下,用户点击下载文件,会发现文件类型失丢的问题,解决方案如下: //IE8下载时,只显示action的名字,没有文件名和后缀 @仰止网Simba //return File(bufferbyte ...

  6. Python——列表、元祖、字典、集合的基本操作

    列表 1. 列表——增 (1)append li = ['xcsd', 'cdc', [1, 5, 2], 'eht', '辛辰'] li.append('nihao') print(li) #['x ...

  7. OpenResty 最佳实践 lua与nginx的结合 --引用自https://moonbingbing.gitbooks.io/openresty-best-practices/content/

    系统的说明了lua在nginx上的开发 请大家到源址查看 OpenResty最佳实践

  8. 第二次作业——分布式版本控制系统Git的安装与使用

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远程仓库地址是:https://github.com/sheep ...

  9. gitea 源码阅读笔记 002 生成无依赖单文件可执行包

    gitea bindata static gitea 可以通过 make generate 生成一个单文件可执行程序, 该文件不需要任何其它依赖,直接可以单独执行. 对于用户的安装.升级和生成dock ...

  10. 层次softmax函数(hierarchical softmax)

    一.h-softmax 在面对label众多的分类问题时,fastText设计了一种hierarchical softmax函数.使其具有以下优势: (1)适合大型数据+高效的训练速度:能够训练模型“ ...