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.
分析:http://www.cnblogs.com/grandyang/p/5188938.html
这道题让我们求一棵二分树的最大二分搜索子树,所谓二分搜索树就是满足左<根<右的二分树,我们需要返回这个二分搜索子树的节点个数。题目中给的提示说我们可以用之前那道Validate Binary Search Tree的方法来做,时间复杂度为O(nlogn),这种方法是把每个节点都当做根节点,来验证其是否是二叉搜索数,并记录节点的个数,若是二叉搜索树,就更新最终结果,参见代码如下:
int largestBSTSubtree(TreeNode root) {
int[] res = new int[];
helper(root, res);
return res[];
}
void helper(TreeNode root, int[] res) { // assume eacho node is the root
if (root == null) return;
int d = count(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
res[] = Math.max(res[], d);
helper(root.left, res);
helper(root.right, res);
} int count(TreeNode root, int mn, int mx) { // check whether it is a BST, if no, return -1, if yes, return its # of nodes.
if (root == null) return ;
if (root.val < mn || root.val > mx) return -;
int left = count(root.left, mn, root.val);
if (left == -) return -;
int right = count(root.right, root.val, mx);
if (right == -) return -;
return left + right + ;
}
O(n)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int largestBSTSubtree(TreeNode root) {
int[] res = new int[];
largestBSTHelper(root, res);
return res[];
} private Data largestBSTHelper(TreeNode root, int[] res) {
Data curr = new Data();
if (root == null) {
curr.isBST = true;
curr.size = ;
return curr;
} Data left = largestBSTHelper(root.left, res);
Data right = largestBSTHelper(root.right, res);
if (left.isBST && root.val > left.max && right.isBST && root.val < right.min) {
curr.isBST = true;
curr.size = + left.size + right.size;
curr.min = Math.min(root.val, left.min);
curr.max = Math.max(root.val, right.max);
res[] = Math.max(res[], curr.size);
} else {
curr.size = ;
}
return curr;
}
} class Data {
boolean isBST = false;
// the minimum for right sub tree or the maximum for right sub tree
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// if the tree is BST, size is the size of the tree; otherwise zero
int size;
}
http://blog.csdn.net/likecool21/article/details/44080779
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: 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 ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ 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节点数最多的bst子树
[抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...
- [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
原题链接在这里: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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
随机推荐
- vs2013使用C#6.0
安装 nuget Microsoft.Net.Compilers
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- SSL、OPENSSL、SSH、OPENSSH
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...
- YII2 实现后台操作记录日志(转)
一.连接linux服务器,创建数据文件 php yii migrate/create user_log 二.修改数据文件 console/migrations/m150721_032220_admin ...
- iOS-iOS 获取蓝色文件夹图片
Xcode创建的iOS项目内存在两种文件夹:Group(黄色, 伪文件夹) 和Folder(蓝色, 真文件夹): Group: Folder: Images.xcassets或Group文件夹内的PN ...
- thinkphp伪静态(url重写)
1. 服务器开启url_rewrite功能,linux空间的php虚拟主机只需要开启apache的mod_rewriet,如果是iis6.0就要安装ISAPI Rewrite模块,apache只要开启 ...
- C# 中excel操作
c#中设置Excel单元格格式 1.全表自动列宽 mysheet.Cells.Select(); mysheet.Cells.Columns.AutoFit(); 2.合并 excelRa ...
- windows下PHP+Mysql+Apache环境搭建
Apache版本:httpd-2.2.22-win32-x86-openssl- 下载地址:http://pan.baidu.com/s/1sjuL4RV PHP版 ...
- PHP基础 之 基本数据类型练习
<h3>PHP基础练习</h3> <?php echo "<h4>常量</h4>"; //定义:一般大写,使用下划线间隔 de ...
- 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频
微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...