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 ...
随机推荐
- 属性字符串的replaceCharactersInRange方法
一,实验: 1> 让 range 的 length 参数为0,以下代码输出属性字符串的结果为12354 NSMutableAttributedString *attrStr = [[NSMuta ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Linux_安装软件包
一.软件包: 源码包 二进制包(rpm包,编译完成) 依赖性 包A-->包B-->包C 一.rpm 挂载镜像,从镜像文件中找到要安装的rpm包 [root@hadoop09-linux ~ ...
- ubuntn 安装 MySQL
1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client 3. sudo apt-get install li ...
- php 将查询出的数组数据存入redis
我们从数据库查询出来的数据一般为数组的形式, 而redis是不支持存入数组的, 一种解决办法是将数组转化为json数据,再将json存入redis,之后取出时再将json转化为php数组. 但将取出的 ...
- IE 9 以下兼容HTML5
<head> <meta name="viewport" content="width=device-width,initial-scale=1.0&q ...
- Unity3D中C#和js方法相互调用
通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets". "Pro Standard Assets" ...
- DateFormatUtil.java
package com.vcredit.framework.utils; import java.sql.Timestamp;import java.text.DateFormat;import ja ...
- IOS第二天多线程-01-延时执行
**********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implement ...
- 面向系统管理员的10款Linux GUI工具 (转自51cto)
如果你是名系统管理员,现已到了Linux非知道不可的地步.如果你在更庞大的环境下工作,更是如此.许多企业组织已迁离了一切都借助点击式GUI来管理的Windows.幸好,Linux也有许多GUI工具可以 ...