[leetcode]333. Largest BST Subtree最大二叉搜索树子树
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最大二叉搜索树子树的更多相关文章
- [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- LeetCode:将有序数组转换为二叉搜索树【108】
LeetCode:将有序数组转换为二叉搜索树[108] 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差 ...
- 【LeetCode】333. Largest BST Subtree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 333. Largest BST Subtree节点数最多的bst子树
[抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...
- [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 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode(98): 验证二叉搜索树
Medium! 题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右 ...
随机推荐
- Microsoft Dynamics CRM 2011 安装完全教程
作者:卞功鑫,转载请保留.http://www.cnblogs.com/BinBinGo/p/4302612.html 环境介绍 WINDOWS 2008 R2 Datacenter Microsof ...
- ElasticSearch 索引模块——集成IK中文分词
下载插件地址 https://github.com/medcl/elasticsearch-analysis-ik/tree/v1.10.0 对这个插件在window下进行解压 用maven工具对插件 ...
- Wndows 下npm 安装依赖时出现错误:MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "4.0".
当在Windows环境中使用npm install或者yarn 安装依赖时,可能会出现如下类似的错误: MSBUILD : error MSB4132: The tools version " ...
- python入门-IF语句
1 格式 cars = ['audi','bmw','subaru','toyata'] for car in cars: if car =='bmw': print(car.upper()) els ...
- CUDA C Programming Guide 在线教程学习笔记 Part 5
附录 A,CUDA计算设备 附录 B,C语言扩展 ▶ 函数的标识符 ● __device__,__global__ 和 __host__ ● 宏 __CUDA_ARCH__ 可用于区分代码的运行位置. ...
- jQuery id模糊 选择器 批量处理
$("span[id^='province_']").each(function(index,obj){ $(obj).bind("click", ...
- 全文检索在 MySQL
中就是一个 FULLTEXT 类型索引.FULLTEXT 索引用于 MyISAM 表,可以在 CREATE TABLE 时或之后使用 ALTER TABLE 或 CREATE INDEX 在 CHAR ...
- Git 代码版本还原方法
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Git 代码版本还原方法 在使用 Git 管理自己的代码和资料时,难免会遇到意料 ...
- hive 下篇
由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...
- Node MonGoDb 简单的增删改查
let MongoClient = require("mongodb").MongoClient; let url = "mongodb://192.168.200.10 ...