Leetcode: Largest BST Subtree
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.
Here's an example:
10
/ \
5 15
/ \ \
1 8 7
The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.
Hint: You can recursively use algorithm similar to 98. Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?
refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2
这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能parent并不能构成BST,反倒是需要如果subtree是BST的话,限制parent的取值范围,然后根据情况判断是:
1. 吸纳parent以及parent的另一个subtree进来形成一个更大的BST,向上传递这个新subtree的size
2. parent向上传递自它以下发现的最大BST
所以我们需要传递subtree size, subtree (min, max)范围,所以我们想到用一个wrapper class包括这三项东西。
同时因为要能分辨上面1、2两种情况,所以size为正表示1,size为负表示2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class Result {
int res;
int min;
int max;
public Result(int num, int n1, int n2) {
this.res = num;
this.min = n1;
this.max = n2;
}
} public int largestBSTSubtree(TreeNode root) {
Result res = findLargestBST(root);
return Math.abs(res.res);
} public Result findLargestBST(TreeNode cur) {
if (cur == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
Result left = findLargestBST(cur.left);
Result right = findLargestBST(cur.right);
if (left.res<0 || right.res<0 || cur.val<left.max || cur.val>right.min) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.res))*(-1), 0, 0);
}
else return new Result(left.res+right.res+1, Math.min(left.min, cur.val), Math.max(right.max, cur.val));
}
}
Leetcode: Largest BST Subtree的更多相关文章
- [LeetCode] 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 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [leetcode]333. 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 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 ...
- 333. Largest BST Subtree节点数最多的bst子树
[抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...
随机推荐
- CF 346B. Lucky Common Subsequence(DP+KMP)
这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...
- HDU 4433 locker(SPFA+DP)
题目链接 去年区域赛的题目,早就看过题目了,又是过了好久了... 这题状态转移,一看就知道应该是 线性的那种,不过细节真的不好处理,一直没想出怎么搞,期间也看过题解,好像没太看懂... dp[i][j ...
- linux系统下yum源的搭建
1.建立挂载点 系统默认在 /mnt目录 1>创建挂载点 mkdir -p /mnt/cdrom 参数-p是需要时创建目标目录的上层目录,但即使这些目录已存在也不当作错误处理 2>查看 ...
- 地理数据库的类型geodatabase类型
地理数据库的类型geodatabase类型 地理数据库是用于保存数据集集合的“容器”.有以下三种类型: 文件地理数据库 - 在文件系统中以文件夹形式存储.每个数据集都以文件形式保存,该文件大小最多可扩 ...
- VS开发好用的扩展
VS开发好用的扩展(转) 转自:http://www.haogongju.net/art/1977373 首先为大家介绍一下开发字体,做程序开发,代码可读性,在侧面也能帮助开发提高效率,所以给大家介绍 ...
- eclipse配置PHP开发环境
下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JDK,Eclipse 安装需要JDK环境:http:// ...
- C#反射生成简单sql语句
static void Main(string[] args) { book book = new book();//实体类 booktest b1 = new booktest(); book.bo ...
- java-两个大数相加
题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...
- 3G網絡容量和業務承載的壓力大大增加!
在移動通信話音業務繼續保持發展的同時,對IP和高速數據業務的支持已經成為移動通信系統演進的方向.移動數據業務是推動目前移動通信技術發展的主要動力,TD-LTE作為準4G技術,以提高數據速率和頻譜利用率 ...
- 关于c++风格 code style
Header files should be self-contained. All header files should have #define guards to prevent multip ...