题目地址: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++)的更多相关文章

  1. [LeetCode] 333. 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. 333. Largest BST Subtree节点数最多的bst子树

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

  5. 333. Largest BST Subtree

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

  6. 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 ...

  7. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  8. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

随机推荐

  1. PPT——一个有情怀的免费PPT模板下载网站!“优品PPT”

    http://www.ypppt.com/ PS:再推荐一款免费PPT下载网站 https://www.v5ppt.com/ppt-5-42-1.html

  2. Excel-实现选择性粘贴(粘贴公式为文本)自动化,不想手动

    10.选择性粘贴(粘贴公式为文本)自动化,不想手动: (1)参考:https://jingyan.baidu.com/article/20b68a88a8bf55796cec62a3.html (2) ...

  3. vector初始化的几种方式-STL

     vector<int>::iterator int_ite;  vector<string>::iterator string_ite;  //vector<T> ...

  4. 深入了解scanf() getchar()和gets()等函数之间的区别

    scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些函数除了问题,却找不出其中的原因.下面先看一个很简单的程序: 程序1 ...

  5. day20 系统优化

    day20 系统优化 yum源的优化 yum源的优化: 自建yum仓库 使用一个较为稳定的仓库 # 安装华为的Base源 或者使用清华的源也可以 wget -O /etc/yum.repos.d/Ce ...

  6. 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))

    1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...

  7. GPU随机采样速度比较

    技术背景 随机采样问题,不仅仅只是一个统计学/离散数学上的概念,其实在工业领域也都有非常重要的应用价值/潜在应用价值,具体应用场景我们这里就不做赘述.本文重点在于在不同平台上的采样速率,至于另外一个重 ...

  8. 爬虫系列:使用 MySQL 存储数据

    上一篇文章我们讲解了爬虫如何存储 CSV 文件,这篇文章,我们讲解如何将采集到的数据保存到 MySQL 数据库中. MySQL 是目前最受欢迎的开源关系型数据库管理系统.一个开源项目具有如此之竞争力实 ...

  9. ORACLE 本session产生的redo

    select * from v$statname a ,v$mystat bwhere a.STATISTIC# = b.STATISTIC# and a.name = 'redo size';

  10. Can we call an undeclared function in C++?

    Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing argum ...