[抄题]:

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. PythonStudy——数字类型 Number type

    # 了了解:py2中小整数用int存放,大整数用long# 1.整型 num = -1000000000000000000000000000000000000000000000000 print(nu ...

  2. windos下安装django

    一:pip install Django       安装完以后,运行python manager.py runserver 0.0.0.0:8000报错:   1):没有安装Mysql-python ...

  3. Windows 下最佳的 C++ 开发的 IDE 是什么?

    作者:渡世白玉链接:https://www.zhihu.com/question/19589089/answer/30312199来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  4. Hadoop 管理工具HUE配置-Hive配置

    1 前言 首先要配置好Hive,可以参见:http://www.cnblogs.com/liuchangchun/p/4761730.html 2 hive配置 找到beeswax标签,不叫hive, ...

  5. c# 泛型和IComparable<T>接口

    泛型 因为我们在编程中想先不定义数据类型,只想先写逻辑,就可以使用Object类型, 这样我们的逻辑就适用于所有类型,但是,在运行中,Object类型的变量会需要 转换到对应类型,浪费资源,所有出现泛 ...

  6. PHP运行出现Notice : Use of undefined constant 的解决办法

    这些是 PHP 的提示而非报错,PHP 本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示.一般作为正式的网站会把提示关掉的,甚至连错误信息也被关掉 关闭 PHP 提示的方法 搜索php.i ...

  7. [UnityAPI]DataUtility类

    测试环境: 准备三张图片a,b,c,其中a,b打在同一图集,c不打图集,a,b如下: 测试脚本: using UnityEngine; using UnityEngine.Sprites; publi ...

  8. uva-993-贪心

    题意:给你一个数字y,生成另外一个最小的数字x,使得x里面的每一位相乘等于y 解题思路:直接贪心就是,x里面的每一位都小于等于9 #include <string> #include< ...

  9. 作着玩:登录页(纯css,不支持ie9以下)

    支持chrome FireFox 和 IE10+,(IE9也能显示,IE9以下不支持) <style type="text/css"> body{position:re ...

  10. activemq 的那些事1

    #关于事务: activemq 遇到的不能消息确认的问题. Session session = connection.createSession(Boolean.FALSE,   Session.AU ...