[LeetCode] 333. 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.
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:
- 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
类似题目:
参考资料:
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
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 333. Largest BST Subtree 最大的二分搜索子树的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 【LeetCode】333. Largest BST Subtree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 333. Largest BST Subtree节点数最多的bst子树
[抄题]: Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where large ...
- 333. Largest BST Subtree
nlgn就不说了..说n的方法. 这个题做了好久. 一开始想到的是post-order traversal. 左右都是BST,然后自己也是BST,返还长度是左+右+自己(1). 左右其中一个不是,或者 ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
随机推荐
- 【MySQL配置参数】sync_binlog和innodb_flush_log_at_trx_commit
sync_binlog和innodb_flush_log_at_trx_commit这2个参数都是MySQL中,配置日志持久化时机的,但有很大不同,做下对比分析总结. 1.MySQL服务器配置参数:s ...
- JVM的参数以及作用详解
-XX:+PrintCommandLineFlags 打印出JVM运行时的各种的各项配置参数 -verbose:gc 发生GC时,打印出GC日志-XX:+printGC 发生GC时,打印出GC ...
- IIS Express 启用目录浏览 方法
标签: iis / visual studio / C# / ASP.NET / .NET 522 今天刚刚使用visual studio 2013创建第一个hello world,结果就发现提示错误 ...
- 如何搭建wordpress ,wecenter
14.什么是LNMP架构 LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写.L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可 ...
- CTF必备技能丨Linux Pwn入门教程——调整栈帧的技巧
Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...
- SQLi-LABS Page-2 (Adv Injections) Less30-Less35
Less-30 GET - BLIND - IMPIDENCE MISMATCH- Having http://10.10.202.112/sqli/Less-30?id=1" #false ...
- 深入Java源码剖析之Set集合
Java的集合类由Collection接口和Map接口派生,其中: List代表有序集合,元素有序且可重复 Set代表无序集合,元素无序且不可重复 Map集合存储键值对 那么本篇文章将从源码角度讨论一 ...
- Android软件架构
08_29_Android软件架构 架构的本质 本质, 类似图纸, 不是建筑物: 明确范围 软件设计中, 架构不等于框架: 底层的编码,到设计模式, 到框架,再到架构(微服务,SOA) 好的架构 做好 ...
- EM算法-完整推导
前篇已经对EM过程,举了扔硬币和高斯分布等案例来直观认识了, 目标是参数估计, 分为 E-step 和 M-step, 不断循环, 直到收敛则求出了近似的估计参数, 不多说了, 本篇不说栗子, 直接来 ...
- itextpdf5单元格中的段落没有行间距
关于对表格中的段落没有行间距的解决方式:通过观察和推测的结论:itextpdf对一些属性,只会对最外层元素的属性进行接收处理,如行间距.例: Paragraph p = new Paragraph(1 ...