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.

题意:

给定二叉树,找出最大的二叉搜索树子树。

思路:

代码:

 class Solution {
public int largestBSTSubtree(TreeNode root) {
if(root == null) return 0;
return dfs(root)._size;
} private Result dfs(TreeNode root){
if(root == null) return new Result (true, 0, 999, -999); Result leftResult = dfs(root.left);
Result rightResult = dfs(root.right); boolean isBST = ((root.right == null ||(rightResult._isBST) && (rightResult._min > root.val)) && (root.left == null || (leftResult._isBST)&&(leftResult._max < root.val )));
int size = isBST ? (leftResult._size + rightResult._size +1) : Math.max(leftResult._size, rightResult._size);
int min = root.left == null ? root.val : leftResult._min;
int max = root.right == null ? root.val : rightResult._max;
return new Result(isBST, size, min, max); } private class Result{
boolean _isBST;
int _size;
int _min;
int _max;
private Result(boolean isBST, int size, int min, int max){
_isBST = isBST;
_size = size;
_min = min;
_max = max;
}
}
}

[leetcode]333. Largest BST Subtree最大二叉搜索树子树的更多相关文章

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

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

  2. LeetCode 333. Largest BST Subtree

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

  3. LeetCode:将有序数组转换为二叉搜索树【108】

    LeetCode:将有序数组转换为二叉搜索树[108] 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差 ...

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

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

  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] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. LeetCode(98): 验证二叉搜索树

    Medium! 题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右 ...

随机推荐

  1. (转)手机的AP和BP是什么?

    AP:Application Processor,即应用芯片 BP:Baseband Processor,即基带芯片 搞什么嘛,双核就双核呗,怎么又搞出个AP和BP啊 原来,FCC(美国联邦通信委员会 ...

  2. Spark学习笔记5:Spark集群架构

    Spark的一大好处就是可以通过增加机器数量并使用集群模式运行,来扩展计算能力.Spark可以在各种各样的集群管理器(Hadoop YARN , Apache Mesos , 还有Spark自带的独立 ...

  3. Vuex 状态管理模式

    Vuex 是一个专为 Vue.js 设计的状态管理模式 vuex解决了组件之间同一状态的共享问题.当我们的应用遇到多个组件共享状态时,会需要: 多个组件依赖于同一状态.传参的方法对于多层嵌套的组件将会 ...

  4. SQL Server 2008 CDC增量变更捕获详解

    1 背景: 随着公司业务的成长,数据量也随之的不断增长.随之而来的问题是在做ETL的时候,时间花费也越来越长.为了节省时间开销,我们只想要更新最新的数据,不想要把公司历年所有的数据都进行处理.这种情况 ...

  5. Installation of Scylla on CentOS 7

    Scylla on CentOS 7 Use these steps to install Scylla using Yum repositories on CentOS. Prerequisites ...

  6. docker容器修改时区(java应用log信息与标准容器时间有八个小时时间差)

    在docker容器中运行的java应用打出的日志时间和通过date -R方式获取的容器标准时间有八个小时时间差- 因为docker容器的原生时区为0时区,为了和国内时区保持一致,需要把容器时区调为东八 ...

  7. scala操作hbase案例

    案例取自streaming-app项目 package com.asiainfo.ocdc.streaming.tools import org.apache.hadoop.hbase.HBaseCo ...

  8. 单例模式(Singleton)

    单例模式  Singletonn Pattern Ensure a class has only one instance, and provide  a global point of access ...

  9. CYQ.Data 数据框架 使用篇一 入门指南---001

    原文链接:http://www.cyqdata.com/cyqdata/article-detail-411 本文针对V5版本进行修改于(2016-07-04) 下面是使用步骤: 一:下载框架DLL[ ...

  10. vconsole h5应用ajax请求抓包

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...