【LeetCode】333. Largest BST Subtree 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.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.
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?
题目大意
找出一个树中最大的BST有多少个节点。
解题方法
DFS
如果用简单的方法做,就是先判断每个树是不是BST,如果是则返回其节点个数,如果不是就递归他的左右子树,最后结果要用最大值。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
if (!root) return 0;
int res = 0;
if (isBST(root, INT_MIN, INT_MAX)) {
return countNodes(root);
}
res = max(res, largestBSTSubtree(root->left));
res = max(res, largestBSTSubtree(root->right));
return res;
}
bool isBST(TreeNode* root, int minVal, int maxVal) {
if (!root) return true;
if (root->val >= maxVal)
return false;
if (root->val <= minVal)
return false;
return isBST(root->left, minVal, root->val) && isBST(root->right, root->val, maxVal);
}
int countNodes(TreeNode* root) {
if (!root) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
private:
int res = 0;
};
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】333. Largest BST Subtree 解题报告(C++)的更多相关文章
- [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最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 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 812 Largest Triangle Area 解题报告
题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- [LeetCode] 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 ...
随机推荐
- 巩固javaweb的第二十三天
巩固内容: 调用验证方法 验证通常在表单提交之前进行,可以通过按钮的 onClick 事件,也可以通过 form 表单 的 onSubmit 事件来完成. 本章实例是通过 form 表单的 onSub ...
- 断言(assert)简介
java中的断言assert的使用 一.assertion的意义和用法 J2SE 1.4在语言上提供了一个新特性,就是assertion功能,他是该版本再Java语言方面最大的革新. 从理论上来说,通 ...
- 【Reverse】初遇花指令
解密花指令 全文参考了一个大师傅的blog:https://blog.csdn.net/zhangmiaoping23/article/details/38400393 介绍 花指令是对抗反汇编的有效 ...
- 【leetocode】55. Jump Game
You are given an integer array nums. You are initially positioned at the array's first index, and ea ...
- 案例 stm32的dma传输过程
首先说一下:DMA_GetCurrDataCounter返回值是什么 返回值是dma缓存里还剩余多少空间. 上面本来应该是,发一下,改变一下.但是这里有一行是特殊的. long : 461,*ff l ...
- 【Git项目管理】git新手入门——基础教程
一.Git工作流程 直接上手看图,了解Git工具的工作流程: 以上包括一些简单而常用的命令,但是先不关心这些,先来了解下面这4个专有名词. Workspace:工作区 Index / Stage:暂存 ...
- Android EditText软键盘显示隐藏以及“监听”
一.写此文章的起因 本人在做类似于微信.易信等这样的聊天软件时,遇到了一个问题.聊天界面最下面一般类似于如图1这样(这里只是显示了最下面部分,可以参考微信等),有输入文字的EditText和表情按钮等 ...
- shell 截取字符串实例教程
本节内容:shell字符串截取方法 1,去掉字符串最左边的字符 [root@jbxue ~]$ vi test.sh 1 STR="abcd" 2 STR=${STR#" ...
- Alamofire-5.0.0 以上报错
摘要 Alamofire 更新到新版本时,遇到了两个错误和一个警告️,所以记录下来它们,以及如何解决它们.给其他出现类似问题的同道一些解决的方向. 今天新开启一个项目,因为网络请求选择 Alamofi ...
- 【笔记】草履虫也能看懂的ELK搭建流程
环境需要 Elasticsearch需要JAVA环境,至少是JDK1.8 elasticsearch 不允许root用户使用,需要新增个elk用户 如果觉得官网下载太慢,可以使用这个 https:// ...