原题链接在这里: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. Loadrunder脚本篇——web_custom_request函数介绍

    c语言版本: int web_custom_request(const char *RequestName, , [EXTRARES, ,] LAST ); 参数说明: RequestName     ...

  2. Linux系统资源查询命令(cpu、io、mem)

    cpu/mem: 1. 指定pid top -p pid1,pid2,... 2. top排序 先top,然后  输入大写P,则结果按CPU占用降序排序.输入大写M,结果按内存占用降序排序. io: ...

  3. 【HackerRank】The Love-Letter Mystery

    James找到了他的朋友Harry要给女朋友的情书.James很爱恶作剧,所以他决定要胡搞一下.他把信中的每个单字都变成了回文.对任何给定的字符串,他可以减少其中任何一个字符的值,例如'd'可以变成' ...

  4. Linux虚拟内存管理(glibc)

    转:https://blog.csdn.net/tengxy_cloud/article/details/53067396 https://www.cnblogs.com/purpleraintear ...

  5. 优美的英文诗歌Beautiful English Poetry

    <When you are old>——<当你老了> --- William Butler Yeats ——威廉·巴特勒·叶芝When you are old and grey ...

  6. Pytorch的gather用法理解

    先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...

  7. POJ 2431 贪心+优先队列

    题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点.   思路:运用优先队列,将能走到的加油站的油量加入优先队列中, ...

  8. python脚本发送电子邮件

    #!/usr/bin/pythonimport smtplibimport stringHOST='smtp.qq.com'#HOST='mail.qq.com'SUBJECT='Test email ...

  9. shell脚本:批量修改文件名

    参考链接1:shell脚本:批量修改文件名(删除文件名中字符) 参考链接2:linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 ) 参考链接3:每天一个linux ...

  10. java深入探究15-SpringMVC

    测试代码:链接:http://pan.baidu.com/s/1c1QGYIk 密码:q924 回顾spring+struts web.xml配置;struts核心过滤器;spring监听器-> ...