[LeetCode] 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] Largest BST Subtree 最大的二分搜索子树的更多相关文章
- [LeetCode] 333. Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 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 ...
- [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
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- 【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 ...
- Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [Locked] Largest BST Subtree
Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...
随机推荐
- Eclipse 实用技巧
代码智能提示 Java智能提示 Window -> Preferences -> Java -> Editor -> Content Assist -> Auto Act ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- Scala快速概览
IDEA工具安装及scala基本操作 目录 一. 1. 2. 3. 4. 二. 1. 2. 3. 三. 1. 2. 3. 4. 5. 6. 7. 四. 1. (1) (2) (3) (4) (5) ( ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- C#——字段和属性
//我的C#是跟着猛哥(刘铁猛)(算是我的正式老师)<C#语言入门详解>学习的,微信上猛哥也给我讲解了一些不懂得地方,对于我来说简直是一笔巨额财富,难得良师! 在刚开始学习属性这一节时,开 ...
- source /etc/profile报错-bash: id:command is not found
由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...
- 工业物联网或系统集成中应用消息队列(ActiveMQ,C#的demo)的场景全面分析
1.[连载]<C#通讯(串口和网络)框架的设计与实现> 2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 2.应用SuperIO(SIO)和开源跨平台物联网框 ...
- css知多少(11)——position
1. 引言 本文将用一篇文章介绍position(定位),在学习position之前,我们应该去思考一个问题:什么情况下我们需要定位?如果没有定位将无法满足我们怎样的需求?我们要知道,被人类创造出来的 ...
- BFC布局
这几天都没有写博客,自己的懒惰又要跑出来了,发觉不能再这样下去了,不然就什么都不想干了,然后将之前已经写得差不多的博客重新检视了一遍.这篇博客已经写得挺久的了,但是一直没有发布,现在补充了一些,也让自 ...
- xcode8.0升级之后公司项目遇到的问题
xcode8升级之后项目遇到了问题,由于这个项目是我中途接手的,遇到的第三方也是自己没有用过的, AQGridViewCell,这个第三方的类主要是用于处理图片的问题,xcode开发工具升级过后,报这 ...