LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/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.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?
题解:
采用bottom-up的方法,简历新的class, 用来存储
- 当前节点为root的subtree是否是BST
- 若是,最小val 和最大val.
- size是当前subtree的大小.
然后从下到上更新,若是中间过程中size 比 res大,就更新res.
Time Complexity: O(n). 每个点不会访问超过两遍. Space: O(logn). Recursion stack space.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int largestBSTSubtree(TreeNode root) {
int [] res = {0};
helper(root, res);
return res[0];
} private Node helper(TreeNode root, int [] res){
Node cur = new Node();
if(root == null){
cur.isBST = true;
return cur;
}
Node left = helper(root.left, res);
Node right = helper(root.right, res);
if(left.isBST && root.val > left.max && right.isBST && root.val < right.min){
cur.isBST = true;
cur.min = Math.min(root.val, left.min);
cur.max = Math.max(root.val, right.max);
cur.size = left.size + right.size + 1;
if(cur.size > res[0]){
res[0] = cur.size;
}
}
return cur;
}
} class Node{
boolean isBST;
int min;
int max;
int size;
public Node(){
isBST = false;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
size = 0;
}
}
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最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 【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 ...
- 333. Largest BST Subtree
nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
随机推荐
- position:absolute width
position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...
- Spring_HelloWord
环境:IntelliJ 14 : jdk1.8 Spring操作步骤 1.新建项目---Spring Batch 2.IntelliJ会自动加载jar包 3.现在就可以在src目录下写Java类文 ...
- vuex的 例子
最近在学习vuejs,一直有听说vuex,用来实现多组件共享的一种状态管理模式,但是网上都说,不要为了用vuex而用vuex,大概意思就是尽量少用vuex,一些小项目可以用bus来实现组件之间的传值问 ...
- token的生成和应用
token的生成和应用 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: ...
- CSS3手风琴下拉菜单
在线演示 本地下载
- python爬虫之html解析Beautifulsoup和Xpath
Beautiifulsoup Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.BeautifulSoup 用来解析 HTML 比较简 ...
- c#.NET中日志信息写入Windows日志中解决方案
1. 目的应用系统的开发和维护离不开日志系统,选择一个功能强大的日志系统解决方案是应用系统开发过程中很重要的一部分.在.net环境下的日志系统解决方案有许多种,log4net是其中的佼佼者.在Wind ...
- 【转】Android BitmapShader 实战 实现圆形、圆角图片
转载自:http://blog.csdn.net/lmj623565791/article/details/41967509 1.概述 记得初学那会写过一篇博客Android 完美实现图片圆角和圆形( ...
- skynet中动态库的处理
skynet中的.so动态库由service-src中的c文件编译完后生成,其中最重要的是snlua.c. 源码地址:https://github.com/cloudwu/skynet/service ...
- VC SOCKET 压缩通信学习
Server................// Server.cpp : Defines the entry point for the console application. // #inclu ...