题目地址: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. 关于单倍型和Phasing

    单倍型,即单倍体基因型,概念很好理解. 单倍型分型的过程就称之Phasing,定相或基因分型. Phasing的意义,在人类疾病遗传和动植物群体遗传中非常重要.也是imputation的必经过程. v ...

  2. 3.Median of Two Sorted Arrays Leetcode

    难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...

  3. Struts 2 基础篇【转】

    转载至 : http://www.blogjava.net/huamengxing/archive/2009/10/21/299153.html Struts2架构图 有视频讲解百度一下就可以找到了 ...

  4. IDEA2021.2安装与配置

    https://blog.csdn.net/qq_37242720/article/details/119349394

  5. Linux定时任务crontable简介

    Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...

  6. 转 关于HttpClient,HttpURLConnection,OkHttp的用法

    转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...

  7. Linux学习 - 流程控制

    一.if语句 1 单分支if条件语句 (1) if  [ 条件判断式 ];then 程序  fi (2) if [ 条件判断式 ] then 程序  fi 例:检测根分区的使用量 2 双分支if条件语 ...

  8. Spring中的InitializingBean与DisposableBean

    InitializingBean顾名思义,应该是初始化Bean相关的接口. 先看一下该接口都定义了哪些方法: public interface InitializingBean { void afte ...

  9. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  10. 【编程思想】【设计模式】【创建模式creational】Borg/Monostate

    Python版 https://github.com/faif/python-patterns/blob/master/creational/borg.py #!/usr/bin/env python ...