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. [UE4]Selector和Sequence的区别

    Selector和Sequence子节点都是返回true才会执行下一个子节点. Sequence是从左到右依次执行,左边节点如果返回false,则不会执行右边的节点 Selector会同步执行所有子节 ...

  2. DRL前沿之:Benchmarking Deep Reinforcement Learning for Continuous Control

    1 前言 Deep Reinforcement Learning可以说是当前深度学习领域最前沿的研究方向,研究的目标即让机器人具备决策及运动控制能力.话说人类创造的机器灵活性还远远低于某些低等生物,比 ...

  3. 进程之间的数据共享 -----Manager模块

    展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据. 这样极大地减少了对使用锁定和其他同步手段的需求,还可以扩展到分布式系统中 ...

  4. pycharm最新版新建工程没导入本地包问题:module 'selenium.webdriver' has no attribute 'Firefox'

    前言 最新版的pycharm做了很大的改变,新建工程的时候,默认不导入本地的安装包,这就导致很多小伙伴踩坑了... 明明已经pip安装过selenium了,但是却报AttributeError:mod ...

  5. django-媒体文件,图片存储

    1.settings.py # 媒体文件 MEDIA_ROOT = 'media/'

  6. linux开发模式

    linux已被使用vim[文本编辑]+gcc[编译]+[gdb代码调试]开发模式 简单设置下开发环境,像设定vim的语法高亮,编辑c时代码自动缩进,tab缩进字符,显示行号等 编辑vinrc一般vin ...

  7. C# IIS 服务器 HTTP 错误 500.21 - Internal Server Error 解决办法

    <1> 管理员身份启动控制台 <2> 输入 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe ...

  8. shutil模块(高级的文件copy)

    import shutil import os f1 = open('本节笔记.txt', encoding = 'utf-8') f2 = open('笔记2', 'w', encoding = ' ...

  9. Spring MVC 运行流程图

  10. as3 三行三列 布满9个为一个界面

    var n:int=int(iconIndex/3); e.x =(int(n/3)*3+iconIndex%3)*557; e.y = int(iconIndex / 3) % 3 * 260; i ...