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. keras中调用tensorboard:from keras.callbacks import TensorBoard

    from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn ...

  2. 第3课 进化后的 const分析

    1.  C语言中的const (1)const修饰的变量是只读的,使得变量具有只读属性,但本质还是变量.所以不是真正的常量,它只是告诉编译器该变量不能出现在赋值符号的左边. (2)const修饰的局部 ...

  3. sklearn-MultinomialNB朴素贝叶斯分类器

    原型 class sklearn.naive_bayes.MultinomialNB(alpha=1.0, fit_prior=True, class_prior=None) 参数 Parameter ...

  4. mysql数据表自动导为python sqlalchemy可操作对象

      1.pip install sqlacodegen pip install pymysql 在/usr/lib/python/site-packages/sqlacodegen/main.py中添 ...

  5. Executor框架(三)线程池详细介绍与ThreadPoolExecutor

    本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介   java.uitl.c ...

  6. redis实现分布式锁 转自importnew 记录一下

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  7. smokeping配置方法

    smokeping配置加Nginx https://lala.im/2821.html  (不完整)

  8. leetcode949

    public class Solution { public string LargestTimeFromDigits(int[] A) { ); ; ; foreach (var nums in l ...

  9. 21OGNL与ValueStack(VS)-静态方法访问

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在LoginAction中增加如下方法: public static Str ...

  10. 使用MATPLOTLIB 制图(小图)

    import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfil ...