[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! 题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右 ...
随机推荐
- jquery的相关应用
1.jQuery获取鼠标事件源(万能) 1 //任意位置 2 3 $(document).ready(function(){ 4 5 $(document).click(function(){ 6 $ ...
- Session的使用与Session的生命周期
1.HttpSession的方法 Object getAttribute(String); Enumeration<String> getAttributeNames(); long ge ...
- Maven web项目启动出错
问题描述: 在第一次建立maven项目后,启动测试,结果jsp页面报告404错误 问题解决: 在pom.xml中引入servlet-api 和 jsp-api,maven不会自动的添加,因此,要在首次 ...
- redis windows dll 下载
https://pecl.php.net/package/redis http://blog.csdn.net/leesin2011/article/details/72801629 http://w ...
- Python中属性和描述符的简单使用
Python的描述符和属性是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的 ...
- Pycharm code templates自定义
Settings>Editor>Code Style>File and Code Templates python script>>>> # 模板变量 ${P ...
- Redis-基本数据类型与内部存储结构
1-概览 Redis是典型的Key-Value类型数据库,Key为字符类型,Value的类型常用的为五种类型:String.Hash .List . Set . Ordered Set 2- Redi ...
- node多进程
内容: 1.多进程与多线程 2.node中多进程相关模块的使用 1.多进程与多线程 多线程:性能高:复杂.考验程序员 多进程:性能略低:简单.对程序员要求低 Node.js中默认:单进程.单线程,但是 ...
- CENTOS 挂载ntfs移动硬盘
参考网址: http://www.it610.com/article/3368930.htm (较全)http://blog.51cto.com/ultrasql/1927672
- mock——test 基本所有使用
可以参考:http://www.cnblogs.com/lyy-2016/p/6122144.html test /** * */ package com.imooc.web.controller; ...