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 ...
随机推荐
- PythonStudy——Python 内置函数 Built-in function
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- [随笔][Golang][golang nil 相关]
nil 是不能比较的 不同类型的nil的address是一样的 不同类型的nil是不能比较的 nil 是map, slice, pointer, channel, func, interface的零值 ...
- 记录安装 java 环境,部署环境变量遇到的小坑
情况:先安装 jdk 7,再安装 jdk8,发现 java 的环境自动变成了 jdk8 解决: 1.在 系统的环境变量下,多出了一行: C:\Program Files (x86)\Common Fi ...
- (C#)中的DataSet、string、DataTable等对象转换成Json
ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...
- 【C++】boost::shared_ptr boost::make_shared
一.shared_ptr shared_ptr作为一个动态分配的对象,当最后一个指向其内容的指针销毁(destroyed)或重置(reset),其指向的内容会被销毁(deleted).不再需要显式调用 ...
- Windows配置ffmpeg
一.ffmpeg简介 ffmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案. 支持操作系统: ...
- if else和switch case那个效率更高一点
switch...case写法: switch (表达式){ case 值1 : 语句1 break; case 值2 : 语句2 break; ... default : 语句n break; } ...
- 《Celeste》 开发者是如何精心制作“冲刺”的
转自:http://www.gameres.com/804804.html 简介与序曲 在Celesete里,许多细微的行动都是发生在转瞬之间的,甚至往往比你想象中还要“转瞬之间”. 这里是 [游戏机 ...
- 用kettle从mysql中使用存储过程读取数据写入到sqlserver数据库
1.mysql存储过程,可以实现动态表读取,满足较为复杂的业务逻辑 DROP PROCEDURE if exists p_get_car_trace; delimiter // CREATE PROC ...
- 第24课 可变参数模板(5)_DllHelper和lambda链式调用
1. dll帮助类 (1)dll的动态链接 ①传统的调用方式:先调用LoadLibrary来加载dll,再定义函数指针类型,接着调用GetProcAddress获取函数地址.然后通过函数指针调用函数, ...