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. Effective Objective-C 2.0 — 第9条:以“类族模式”隐藏实现细节

    第9条:以“类族模式”隐藏实现细节 类族模式可以把实现细节隐藏在一套简单的公共接口后面 系统框架中经常使用类族 从类族的公共抽象基类中继承子类时要当心,若有开发文档,则应首先阅读 “类族”(class ...

  2. OC-类方法

    类方法 1. 基本概念 直接可以用类名来执行的方法(类本身会在内存中占据存储空间,里面有类\对象方法列表) 2. 类方法和对象方法对比 1)  对象方法 以减号-开头 只能让对象调用,没有对象,这个方 ...

  3. java正则表达式 --简单认识

    学习目标 正则表达式的作用正则表达式的模式匹配Pattern类和Matcher类的使用掌握String对正则的支持具体内容一.认识正则(为什么要有正则) 方便的对数据进行匹配 执行复杂的字符串验证.拆 ...

  4. JS网页加载进度条

    参考:http://www.cnblogs.com/timy/archive/2011/12/07/2279200.html

  5. TMS320C64X+ 中使用EDMA3中断

    关于EDMA3,TI的文档有详细的介绍.园子里的文章 http://www.cnblogs.com/fpga/archive/2009/10/13/1582516.html,http://www.cn ...

  6. PHP实现各种经典算法

    <?  //--------------------  // 基本数据结构算法 //--------------------  //二分查找(数组里查找某个元素)  function bin_s ...

  7. 大熊君大话NodeJS之------Global Objects全局对象

    一,开篇分析 在上个章节中我们学习了NodeJS的基础理论知识,对于这些理论知识来说理解是至关重要的,在后续的章节中,我们会对照着官方文档逐步学习里面的各部分模块,好了该是本文主角登台亮相的时候了,G ...

  8. poj 3744 Scout YYF I(概率dp,矩阵优化)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 Descr ...

  9. 【C语言入门教程】7.2 结构体数组的定义和引用

    7.2 结构体数组的定义和引用 当需要使用大量的结构体变量时,可使用结构体定义数组,该数组包含与结构体相同的数据结构所组成的连续存储空间.如下例所示: struct student stu_a[50] ...

  10. Mac 如何安装Homebrew?

    到Github官网上搜索Homebrew,找到对应的Homebrew后,查看它的安装文档,链接如下: https://github.com/Homebrew/homebrew/blob/master/ ...