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.

Follow up:
Can you figure out ways to solve it with O(n) time complexity?

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.

这道题让我们求一棵二分树的最大二分搜索子树,所谓二分搜索树就是满足左<根<右的二分树,需要返回这个二分搜索子树的节点个数。题目中给的提示说可以用之前那道 Validate Binary Search Tree 的方法来做,时间复杂度为 O(n2),这种方法是把每个节点都当做根节点,来验证其是否是二叉搜索数,并记录节点的个数,若是二叉搜索树,就更新最终结果,参见代码如下:

解法一:

class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
int res = ;
dfs(root, res);
return res;
}
void dfs(TreeNode *root, int &res) {
if (!root) return;
int d = countBFS(root, INT_MIN, INT_MAX);
if (d != -) {
res = max(res, d);
return;
}
dfs(root->left, res);
dfs(root->right, res);
}
int countBFS(TreeNode *root, int mn, int mx) {
if (!root) return ;
if (root->val <= mn || root->val >= mx) return -;
int left = countBFS(root->left, mn, root->val);
if (left == -) return -;
int right = countBFS(root->right, root->val, mx);
if (right == -) return -;
return left + right + ;
}
};

下面我们来看一种更简洁的写法,对于每一个节点,都来验证其是否是 BST,如果是的话,就统计节点的个数即可,参见代码如下:

解法二:

class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
if (!root) return ;
if (isValid(root, INT_MIN, INT_MAX)) return count(root);
return max(largestBSTSubtree(root->left), largestBSTSubtree(root->right));
}
bool isValid(TreeNode* root, int mn, int mx) {
if (!root) return true;
if (root->val <= mn || root->val >= mx) return false;
return isValid(root->left, mn, root->val) && isValid(root->right, root->val, mx);
}
int count(TreeNode* root) {
if (!root) return ;
return count(root->left) + count(root->right) + ;
}
};

题目中的 Follow up 让用 O(n) 的时间复杂度来解决问题,还是采用 DFS 的思想来解题,由于时间复杂度的限制,只允许遍历一次整个二叉树,由于满足题目要求的二叉搜索子树必定是有叶节点的,所以思路就是先递归到最左子节点,然后逐层往上递归,对于每一个节点,都记录当前最大的 BST 的节点数,当做为左子树的最大值,和做为右子树的最小值,当每次遇到左子节点不存在或者当前节点值大于左子树的最大值,且右子树不存在或者当前节点值小于右子树的最小数时,说明 BST 的节点数又增加了一个,更新结果及其参数,如果当前节点不是 BST 的节点,那么更新 BST 的节点数 res 为左右子节点的各自的 BST 的节点数的较大值,参见代码如下:

解法三:

class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
int res = , mn = INT_MIN, mx = INT_MAX;
isValidBST(root, mn, mx, res);
return res;
}
void isValidBST(TreeNode* root, int& mn, int& mx, int& res) {
if (!root) return;
int left_cnt = , right_cnt = , left_mn = INT_MIN;
int right_mn = INT_MIN, left_mx = INT_MAX, right_mx = INT_MAX;
isValidBST(root->left, left_mn, left_mx, left_cnt);
isValidBST(root->right, right_mn, right_mx, right_cnt);
if ((!root->left || root->val > left_mx) && (!root->right || root->val < right_mn)) {
res = left_cnt + right_cnt + ;
mn = root->left ? left_mn : root->val;
mx = root->right ? right_mx : root->val;
} else {
res = max(left_cnt, right_cnt);
}
}
};

上面的解法在递归函数中定义了大量的变量,难免让人看的眼花缭乱,我们可以稍稍精简一下,将这些变量都放到递归函数的返回值中,此时的helper函数返回了一个一维数组,里面有三个数字,分别是以当前结点为根结点的数的最小值,最大值,以及最大的 BST 子树的结点个数。那么就可以在边验证 BST 的过程中边统计个数,首先判空,若空,则返回一个默认三元组,整型最大值,最小值,和0。那你可能有疑问,定义的不是说第一个值是最小值么?没错,后面再解释。若当前结点 node 存在,分别对其左右子结点调用递归函数,那么左子树和右子树的信息都保存到了 left 和 right 数组中,就算左右子结点不存在也没关系,由于第一句的判空,还是会得到一个默认的三元组。接下来就是根据左右子树的信息来更新结果 res 了,由于 BST 的定义,当前结点值肯定是大于左子树的最大值,小于右子树的最小值的。左子树的最大值保存在 left[1] 中,右子树的最小值保存在 right[0] 中,如果这两个条件满足了,说明左右子树都是 BST,那么返回的三元组的最小值就是当前结点值和左子树最小值中的较小者,最大值就是当前结点值和右子树最大值中的较大值,返回的 BST 结点个数就是左右子树的结点个数加上1,即算上了当前结点。好,现在解释下为空时返回的三元组为何顺序是整型最大值,整型最小值。如果当前是叶结点,其也算是 BST,那么肯定希望能进入 if 从句,从而使得三元组的第三项能加1,但是 if 的条件是当前结点值要大于左子树中的最大值,现在左子结点是空的,为了保证条件能通过,我们将空的左子树的最大值设置为整型最小值,这样一定能通过,同理,将空的右子树的最小值设置为整型最大值,这就是空结点的三元组的作用。好,继续看 else 中的内容,如果破坏了 BST 的规则,则返回的三元组的最小值就是整型最小值,最大值是整型最大值,BST 结点个数并不是0,因为其左右子树中有可能还有 BST,所以是左右子树中的 BST 结点个数中的较大值,参见代码如下:

解法四:

class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
vector<int> res = helper(root);
return res[];
}
vector<int> helper(TreeNode* node) {
if (!node) return {INT_MAX, INT_MIN, };
vector<int> left = helper(node->left), right = helper(node->right);
if (node->val > left[] && node->val < right[]) {
return {min(node->val, left[]), max(node->val, right[]), left[] + right[] + };
} else {
return {INT_MIN, INT_MAX, max(left[], right[])};
}
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/333

类似题目:

Validate Binary Search Tree

参考资料:

https://leetcode.com/problems/largest-bst-subtree/

https://leetcode.com/problems/largest-bst-subtree/discuss/78892/12ms-C%2B%2B-solution

https://leetcode.com/problems/largest-bst-subtree/discuss/78899/Very-Short-Simple-Java-O(N)-Solution

https://leetcode.com/problems/largest-bst-subtree/discuss/78896/Clean-and-easy-to-understand-Java-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 333. 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 解题报告(C++)

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

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

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

  6. 333. Largest BST Subtree

    nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...

  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. SpringBoot-@ControllerAdvice 拦截异常并统一处理

    SpringBoot是为了简化Spring应用的创建.运行.调试.部署等一系列问题而诞生的产物, 自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可 ...

  2. Python 学习 第17篇:从SQL Server数据库读写数据

    在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...

  3. ADFS登录界面自定义

    最近在做identityServer3+ADFS 实现域账号第三方授权验证,发现一个问题,在我们网站跳转到域账户登录页面,这个页面有点不美观,那么我们改如何自定义这个登录界面呢? ADFS安装配置这里 ...

  4. C# - VS2019WinFrm桌面应用程序FtpClient实现

    前言 本篇主要记录:VS2019 WinFrm桌面应用程序实现简单的FtpClient,包含Ftp文件查看.上传和下载等功能. 准备工作 搭建WinFrm前台界面 添加必要的控件,这里主要应用到Gro ...

  5. Python - 输入和输出 - 第十七天

    Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...

  6. Python urllib与requests、XML和HTMLParser

    参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1019223241745024 Python 的内建模块urllib提供了一系列用于操作 ...

  7. 3-Consul 使用手册

    原文:http://www.liangxiansen.cn/2017/04/06/consul/ Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键 ...

  8. XMind破解版,2019年8月好使

    越来越多的公司用思维导图了,进行编写测试用例,以下为破解版,亲身实验才发 的  ,中国时间2019年8月5日 下载安装包: 链接:https://pan.baidu.com/s/1-ubJLPSEpH ...

  9. Java性能 -- CAS乐观锁

    synchronized / Lock / CAS synchronized和Lock实现的同步锁机制,都属于悲观锁,而CAS属于乐观锁 悲观锁在高并发的场景下,激烈的锁竞争会造成线程阻塞,而大量阻塞 ...

  10. 【设计模式】Adapter

    前言 Adapter设计模式,允许客户端使用接口不兼容的类. 昨天收拾一些以前的东西,发现了藏在柜子里的一条线,这条线叫做OTG.这条线的一端是micro-usb的输出口,另一端是usb的输入口.这条 ...