LeetCode 333. Largest BST Subtree(最大二叉搜索树)

51 篇文章 0 订阅
49 篇文章 0 订阅

原题网址: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.

Hint:

  1. 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?

方法:递归检查。一颗子树当且仅当其左右子树都是BST,并且左子树的最大值小于根节点,右子树的最小值大于根节点的时候,这棵子树才是BST。则定一个递归函数的返回结果,包括该节点能否形成一棵BST,以及该BST的节点总数。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int max = 0;
    private Range check(TreeNode root) {
        Range range = new Range(root.val, root.val);
        Range left = (root.left == null) ? null : check(root.left);
        Range right = (root.right == null) ? null: check(root.right);
        if (left != null) {
            if (!left.bst || left.max >= root.val) return range;
            range.min = left.min;
            range.count += left.count;
        }
        if (right != null) {
            if (!right.bst || root.val >= right.min) return range;
            range.max = right.max;
            range.count += right.count;
        }
        range.bst = true;
        max = Math.max(max, range.count);
        return range;
    }
    public int largestBSTSubtree(TreeNode root) {
        if (root != null) check(root);
        return max;
    }
}

class Range {
    boolean bst;
    int min, max;
    int count = 1;
    Range(int min, int max) {
        this.min = min;
        this.max = max;
    }
}

另一种实现:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    int max = 0;
    private Range find(TreeNode root) {
        Range range = new Range(root.val, root.val, 1);
        Range left = root.left == null ? null : find(root.left);
        Range right = root.right == null ? null : find(root.right);
        if (root.left != null) {
            if (left == null) return null;
            if (left.max >= root.val) return null;
            range.min = left.min;
            range.nodes += left.nodes;
        }
        if (root.right != null) {
            if (right == null) return null;
            if (right.min <= root.val) return null;
            range.max = right.max;
            range.nodes += right.nodes;
        }
        max = Math.max(max, range.nodes);
        return range;
    }
    public int largestBSTSubtree(TreeNode root) {
        if (root == null) return 0;
        find(root);
        return max;
    }
}
class Range {
    int min, max;
    int nodes;
    Range(int min, int max, int nodes) {
        this.min = min;
        this.max = max;
        this.nodes = nodes;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值