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的更多相关文章

  1. [LeetCode] 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. [Locked] Largest BST Subtree

    Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...

  4. [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  5. 333. Largest BST Subtree节点数最多的bst子树

    [抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...

  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. LeetCode 333. Largest BST Subtree

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

  8. [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. 【LeetCode】333. Largest BST Subtree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

随机推荐

  1. Thread 与 Runnable

    在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  2. java中如何将字符串数组转换成字符串(转)

    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...

  3. BeanNameAware接口和BeanFactoryAware接口

    迄今为止,所接触到的Bean都是“无知觉”的,就像黑客帝国中机械工厂里面“养殖”的人类,他们虽然能完成一定的功能,但是根本不知道自己在工厂(BeanFactory)中的代号(id),或者自己是在哪个工 ...

  4. html兼容性

    IE  property:value\9; //for all IE IE6 _property:value; IE7 *property:value; IE8 +property:value; IE ...

  5. oracle中时间运算

    Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...

  6. 【转载】android中.9png

    在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以任意调整大小的一种图片格式“.9.png”.这种图片是用于Android开发的一种特殊的图片格式,它的 ...

  7. 响应性web设计实战总结(二)

    响应性web设计实战总结(二) 阅读目录 背景知识: Gulp-less安装及配置如下 对响应性web总结,之前总结过2篇文章:可以看如下: http://www.cnblogs.com/tugenh ...

  8. javascript数组的知识点讲解

    javascript数组的知识点讲解 阅读目录 数组的基本方法如下 concat() join() pop() push() reverse() shift() sort() splice() toS ...

  9. SQL pivot 基本用法 行列转换 数据透视

    SQL通过pivot进行行列转换 数据透视 可直接在sql server 运行 传统操作 和 pivot create table XKCl (name nchar(10) not null, 学科 ...

  10. 关于QT写注册表开机自启动

    注册表中权限: 1.HKEY_CURRENT_USER 2.HKEY_LOCAL_MACHINE 网上有很多帖子都是用的2,其实这样有违用户权限,而且如果不是管理员用户,会写入不成功! 代码如下: Q ...