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

[抄题]:

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.

Example:

Input: [10,5,15,1,8,null,7]

   10 
   / \ 
  5  15 
 / \   \ 
1   8   7

Output: 3
Explanation: The Largest BST Subtree in this case is the highlighted one.
             The return value is the subtree's size, which is 3.

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

最后结果可能是计算方法正确的负数,还没转过来,所以要再转一次。

[思维问题]:

如果每个点检查valid时都还要traverse,就会形成n2的复杂度。

return isValid(root.left, min, root.val) && isValid(root.right, root.val, max);
所以定义一个新的类,只管自己不管别人。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

方法名和类名保持相同

[一句话思路]:

所以定义一个新的类,只管自己不管别人。因为算法没错,不对时直接*-1转过来就行。
Your input
[10,5,15,1,8,null,7]
Your stdout
left.res = 0
right.res = 1
root.val = 15
left.max = -2147483648
right.min = 7
Math.max(Math.abs(left.res), Math.abs(right.res)) = 1
 
left.res = 3
right.res = -1
root.val = 10
left.max = 8
right.min = 0
Math.max(Math.abs(left.res), Math.abs(right.res)) = 3
 
Your answer
3

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

 

定义一个新的类,只管自己不管别人。因为算法没错,不对时直接*-1转过来就行。

 

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 [是否头一次写此类driver funcion的代码] :

 [潜台词] :

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    class Result {
        int res;
        int min;
        int max;
        
        public Result(int res, int min, int max) {
            this.res = res;
            this.min = min;
            this.max = max;
        }
    }
    
    public int largestBSTSubtree(TreeNode root) {
        Result result = helper(root);
        return Math.abs(result.res);
    }
    
    //divide and conquer, reverse or add to a new num
    public Result helper(TreeNode root) {
        //corner case
        if (root == null) return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
        
        //form left and right
        Result left = helper(root.left);
        Result right = helper(root.right);
        
        //reverse: root's val incorrect or res < 0
        if (root.val < left.max || root.val > right.min || left.res < 0 || right.res < 0) {
            return new Result(Math.max(Math.abs(left.res), Math.abs(right.res)) * (-1), 0, 0);
        }else {
            return new Result(1 + left.res + right.res, Math.min(left.min, root.val), Math.max(right.max, root.val));
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/9503538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值