nlgn就不说了。。说n的方法。

这个题做了好久。

一开始想到的是post-order traversal.

左右都是BST,然后自己也是BST,返还长度是左+右+自己(1)。

左右其中一个不是,或者自己不是的话,返还-1.

第一次尝试忽略了跳级问题,判断自己能不能和左右子树组成BST的情况是,自己必须比左子树的最大值大,比右子树的最小值小。。所以害的自己造个变量记录最大最小值。

提交的时候改了半天,主要问题在于更新最大最小但是会后,既要以子树更新,又要以自己的VAL更新,一开始忽略了其中一种。。

public class Solution
{ public class MaxMin
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
public MaxMin(int max, int min)
{
this.max = max;
this.min = min;
}
} int a = 1;
public int largestBSTSubtree(TreeNode root)
{ if(root == null) return 0;
MaxMin r = new MaxMin(Integer.MIN_VALUE,Integer.MAX_VALUE);
int res = helper(root,r); a = Math.max(res,a); return a; } public int helper(TreeNode root, MaxMin m)
{
if(root == null) return 0;
if(root.left == null && root.right == null)
{
m.max = Math.max(m.max,root.val);
m.min = Math.min(m.min,root.val);
return 1;
} MaxMin m1 = new MaxMin(m.max,m.min);
MaxMin m2 = new MaxMin(m.max,m.min); int left = helper(root.left,m1);
int right = helper(root.right,m2);
int res = 1; m.max = Math.max(m.max,root.val);
m.min = Math.min(m.min,root.val); m.max = Math.max(Math.max(m.max,m1.max),m2.max);
m.min = Math.min(Math.min(m.min,m1.min),m2.min); if(left != -1 && right != -1)
{ if(root.val < m2.min && root.val > m1.max)
{
res += left + right;
a = Math.max(res,a);
return res;
}
else return -1; }
else return -1; } }

另一种N的尝试是,做一个in-order traversal,找其中的递增组合。不过仔细想想犹豫结构不确定,不一定是completed tree所以无法判定。。



二刷。

这个题做了他妈好久。

回头看一刷觉得太繁琐了,肯定有问题,但是又不想在基础上改。

像题目说的,直白的做法是每个NODE都call一次valid BST,这样是O(nlgn).

现在要求O(n),只能遍历一次。那肯定是bottom-up,用post-order traversal.

有一点要注意的是,subtree得到底才行,必须包含最下的leaves.

我们需要传到上面的信息有这么几个:

  1. 我是不是BST,以便于上面判断他是不是BST.

  2. 我作为BST的最大Size,以便于上面进行左右取舍,或相加(如果左右+自己都是BST)

  3. 我的取值区间,最大值和最小值。对于上面的parent node来说,他得比左支最大值大,比右支最小值小。

需要传递的信息有3个,自己建个新的class比较方便。

学到的一个tricky是用size正负来表示1),是否是BST。负数就说明我不是BST。

Time: O(n) DFS post-order

Space: O(n) for Stack in memory

public class Solution {
public class Node {
int max;
int min;
int num;
public Node(int max, int min, int num) {
this.max = max;
this.min = min;
this.num = num;
}
} public int largestBSTSubtree(TreeNode root) {
return Math.abs(dfs(root).num);
} public Node dfs(TreeNode root) {
if (root == null) return new Node(Integer.MIN_VALUE, Integer.MAX_VALUE, 0); Node leftRes = dfs(root.left);
Node rightRes = dfs(root.right); int curMin = Math.min(root.val, leftRes.min);
int curMax = Math.max(root.val, rightRes.max); if (root.val <= leftRes.max || root.val >= rightRes.min || leftRes.num < 0 || rightRes.num < 0) {
//System.out.println(leftRes.num + " " + rightRes.num);
return new Node(curMax, curMin, Math.max(Math.abs(leftRes.num), Math.abs(rightRes.num)) * -1);
} else {
return new Node(curMax, curMin, leftRes.num + rightRes.num + 1);
} }
}

2个判断不太好理解,着重说一下。

    if (root.val <= leftRes.max || root.val >= rightRes.min || leftRes.num < 0 || rightRes.num < 0) {
return new Node(curMax, curMin, Math.max(Math.abs(leftRes.num), Math.abs(rightRes.num)) * -1);
}

这说明不符合BST规定。

root.val <= 左支最大值. root.val >= 右支最小值.

左支BST的SIZE < 0 或者 右支BST的Size < 0

这个时候返还的Node要注意。

curMax和curMin说实话其实无所谓,返还什么都行,因为第三个值要返还负数。

第三个值首先要确定返还负数,最后乘以-1。

判断前取绝对值Math.abs(leftRes.num)是代表:

子树最大subTree的size,不一定就是root.left的值,可能还要往下。

正负号代表当前子树(root.left)是否是BST.

else是判断成功,左右都是,自己也是,那么显然左+右+1就行了。

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. 333. Largest BST Subtree节点数最多的bst子树

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

  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

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

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

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

  6. [LeetCode] 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. Leetcode: Largest BST Subtree

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

  9. [Locked] Largest BST Subtree

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

随机推荐

  1. ResultSetMetaData rsmd = rs.getMetaData()是什么意思?

    ResultSetMetaData rsmt=rs.getMetaData(); 得到结果集(rs)的结构,比如字段数.字段名等.使用rs.getMetaData().getTableName(1)) ...

  2. 在treeview外加一个滚动条的实现

    前台代码: <div style="overflow:auto;width:190px;height:280px;border:1px solid #336699;padding-le ...

  3. 用java写bp神经网络(四)

    接上篇. 在(一)和(二)中,程序的体系是Net,Propagation,Trainer,Learner,DataProvider.这篇重构这个体系. Net 首先是Net,在上篇重新定义了激活函数和 ...

  4. python中如何使用help命令?

    python下 help()使用方法   查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules ...

  5. 转 四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较

                                      现在流行的Java EE容器有很多:Tomcat.JBoss.Resin.Glassfish等等.下面对这四种Java EE容器进行 ...

  6. HTML5 display:inline、block、inline-block的区别--备用

    display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...

  7. Unity图片处理类,包括压缩、截屏和滤镜

    先上代码: 1 using System.Threading; using UnityEngine; using System.IO; using System.Collections; public ...

  8. 通过GetManifestResourceStream加载文件出现错误提示“null值”对于“stream”无效[转]

    本文解决了我的问题,收藏一下. 原文地址:http://blog.sina.com.cn/s/blog_a67799f601010atz.html 在做Mobile开发时,需要引入图片,用到了这个方法 ...

  9. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  10. Oracle OCI-22053:溢出错误

    Oracle 数值数据类型最多可存储 38 个字节的精度.当将 Oracle 数值转换 为公共语言运行库数据类型时,小数点后边的位数可能过多,这会导致此错误.   查询29万笔数据,报此错误,分析应该 ...