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.
Hint: You can recursively use algorithm similar to 98. Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?

refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2

这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能parent并不能构成BST,反倒是需要如果subtree是BST的话,限制parent的取值范围,然后根据情况判断是:

1. 吸纳parent以及parent的另一个subtree进来形成一个更大的BST,向上传递这个新subtree的size

2. parent向上传递自它以下发现的最大BST

所以我们需要传递subtree size, subtree (min, max)范围,所以我们想到用一个wrapper class包括这三项东西。

同时因为要能分辨上面1、2两种情况,所以size为正表示1,size为负表示2

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class Result {
int res;
int min;
int max;
public Result(int num, int n1, int n2) {
this.res = num;
this.min = n1;
this.max = n2;
}
} public int largestBSTSubtree(TreeNode root) {
Result res = findLargestBST(root);
return Math.abs(res.res);
} public Result findLargestBST(TreeNode cur) {
if (cur == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
Result left = findLargestBST(cur.left);
Result right = findLargestBST(cur.right);
if (left.res<0 || right.res<0 || cur.val<left.max || cur.val>right.min) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.res))*(-1), 0, 0);
}
else return new Result(left.res+right.res+1, Math.min(left.min, cur.val), Math.max(right.max, cur.val));
}
}

Leetcode: Largest BST Subtree的更多相关文章

  1. [LeetCode] 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

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

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

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

  4. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

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

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

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

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

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

  7. Largest BST Subtree

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

  8. [Locked] Largest BST Subtree

    Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...

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

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

随机推荐

  1. UVA 558 判定负环,spfa模板题

    1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...

  2. Emoji表情符号录入MySQL数据库报错

    版本一: 1,查看tomcat后台日志,核心报错信息如下:   Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\ ...

  3. The beatles-Yesterday

    轉載自https://www.youtube.com/watch?v=XNnaxGFO18o Yesterday Lyrics:Paul Mccartney Music:Paul Mccartney ...

  4. 20145330第六周《Java学习笔记》

    20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...

  5. Getting the Device GUID

    Sometime you may want to uniquely identify a Windows Phone, such as when you are implementing push n ...

  6. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  7. 理解CSS3 transform中的Matrix(矩阵)

    一.哥,我被你吓住了 打架的时候会被块头大的吓住,学习的时候会被奇怪名字吓住(如“拉普拉斯不等式”).这与情感化设计本质一致:界面设计好会让人觉得这个软件好用! 所以,当看到上面“Matrix(矩阵) ...

  8. zju(6)中断控制实验

    1.实验目的 1.学习和掌握Linux下中断驱动的写法: 二.实验内容 1.编写EduKit-IV实验箱Linux操作系统下按键key的驱动: 2.编写EduKit-IV实验箱Linux操作系统下按键 ...

  9. Suffix array

    A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suf ...

  10. 【iCore3 双核心板_ uC/OS-III】例程十:消息队列

    实验指导书及代码包下载: http://pan.baidu.com/s/1sleklm1 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...