原题链接在这里: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的更多相关文章

  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最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  3. 【LeetCode】333. Largest BST Subtree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  4. 333. Largest BST Subtree节点数最多的bst子树

    [抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...

  5. 333. Largest BST Subtree

    nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...

  6. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  7. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  8. [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

随机推荐

  1. range精讲

    var ec = range.endContainer endContainer不是一个引用类型 range是引用类型 range经过改变范围之后 var ec2 =range.endContaine ...

  2. Oracle表与约束关系

    手动回收表的存储方式: SQL> alter table aux_emp deallocate unused; //回收所有未使用的存储空间 表已更改. 回收aux_emp的存储空间,保留50K ...

  3. Python学习进程(8)字符串內建函数

        Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持.     (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...

  4. keepalived nginx 主备配置

    keepalived  nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...

  5. <script>放在head内和body内有什么区别

    加载的顺序不一样,你可以把HTML看成从上往下加载的. 例如在网速慢的情况下把js代码放在body底部用户会先看到网页结构,等js加载完成后才出现特效 区别简述: 在HTML body部分中的Java ...

  6. C语言math.h库函数中atan与atan2的区别

    源: C语言math.h库函数中atan与atan2的区别 C语言中的atan和atan2

  7. 20145231第二周Java学习笔记

    20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...

  8. pd.read_csv的header用法

    默认Header = 0: In [3]: import pandas as pd In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_d ...

  9. 最短路径Dijkstra模板

    算法思想:把所有的边分成两个集合A,B.集合A表示已经求出最短路径的点,不断扩展集合A,减少集合B.每一扩展就从结合B中找出到源点距离最短的点,加入到A. dis[i]数组代表从出发点到j的距离: m ...

  10. 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)

    题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...