当前位置:网站首页>返回二叉树中最大的二叉搜索子树的大小
返回二叉树中最大的二叉搜索子树的大小
2022-07-02 22:20:00 【明朗晨光】
1、题目
给定一棵二叉树的根节点 root
,返回这棵二叉树中最大的二叉搜索子树的大小
2、分析
一棵二叉树可能它不是二叉搜索树,但是它的子树可能是二叉搜索树。
使用二叉树的递归套路。
①目标:求 X 为根节点的二叉树的最大二叉搜索子树的节点数。
②可能性:
1)X 不做根的情况:
(1)结果在左树上,左树上的maxBSTSubTreeSize;
(2)结果在右树上,右树上的maxBSTSubTreeSize;
2)X 做根的情况。那么整棵树都是BST,需要证明该树是BST,则需要向左右树获取的信息:(1)左树是不是BST;
(2)右树是不是BST;
(3)左树的max < x;
(4)右树的min > x;
(5)左树的size + 右树的size + 1。
整理所有条件的并集可得需要的信息:
(1)maxBSTSubTreeSize;
(2)是否为BST
(3)max
(4)min
(5)size
但是可以化简,如果maxBSTSubTreeSize 和 size 是相等的,那么整棵树就是BST,所以(2)这个信息可以不要。
3、实现
C++ 版
//暂未提交验证
/************************************************************************* > File Name: 038.返回二叉树最大二叉搜索子树的大小.cpp > Author: Maureen > Mail: [email protected] > Created Time: 六 7/ 2 12:47:20 2022 ************************************************************************/
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int data) : value(data) {
}
};
class Info {
public:
int maxBSTSubTreeSize;//最大搜索二叉树的节点数
int maxVal; //树的最大值
int minVal; //树的最小值
int allSize; //树的节点数
Info(int mbs, int ma, int mi, int all) :
maxBSTSubTreeSize(mbs), maxVal(ma), minVal(mi), allSize(all) {
}
};
Info *process(TreeNode *x) {
if (x == nullptr) {
return nullptr;
}
Info *leftInfo = process(x->left);
Info *rightInfo = process(x->right);
int maxVal = x->value;
int minVal = x->value;
int allSize = 1;
int p1 = -1;
int p2 = -1;
int p3 = -1;
if (leftInfo != nullptr) {
maxVal = max(maxVal, leftInfo->maxVal);
minVal = min(minVal, leftInfo->minVal);
allSize += leftInfo->allSize;
//可能性1:答案在左树
p1 = leftInfo->maxBSTSubTreeSize;
}
if (rightInfo != nullptr) {
maxVal = max(maxVal, rightInfo->maxVal);
minVal = min(minVal, rightInfo->minVal);
allSize += rightInfo->allSize;
//可能性2:答案在右树
p2 = rightInfo->maxBSTSubTreeSize;
}
//可能性3:整棵树是BST
bool leftBST = leftInfo == nullptr ? true : leftInfo->maxBSTSubTreeSize == leftInfo->allSize;
bool rightBST = rightInfo == nullptr ? true : rightInfo->maxBSTSubTreeSize == rightInfo->allSize;
if (leftBST && rightBST) {
bool legalLeft = leftInfo == nullptr ? true : leftInfo->maxVal < x->value;
bool legalRight = rightInfo == nullptr ? true : rightInfo->minVal > x->value;
if (legalLeft && legalRight) {
p3 = (leftInfo == nullptr ? 0 : leftInfo->allSize) + (rightInfo == nullptr ? 0 : rightInfo->allSize) + 1;
}
}
int maxBSTSubTreeSize = max(max(p1, p2), p3);
return new Info(maxBSTSubTreeSize, maxVal, minVal, allSize);
}
int largestBSTSubTree(TreeNode *root) {
if (root == nullptr) return 0;
return process(root)->maxBSTSubTreeSize;
}
Java 版
public class MaxSubBSTSize {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
}
}
public static int largestBSTSubtree(Node head) {
if (head == null) {
return 0;
}
return process(head).maxBSTSubTreeSize;
}
public static class Info {
public int maxBSTSubTreeSize; //最大搜索二叉树的节点数
public int allSize; //树的节点数
public int max; //树的最大值
public int min; //树的最小值
public Info(int m, int a, int ma, int mi) {
maxBSTSubTreeSize = m;
allSize = a;
max = ma;
min = mi;
}
}
public static Info process(Node x) {
if (x == null) return null;
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.value;
int min = x.value;
int allSize = 1; //节点x本身
if (leftInfo != null) {
max = Math.max(leftInfo.max, max);
min = Math.max(leftInfo.min, min);
allSize += leftInfo.allSize;
}
if (rightInfo != null) {
max = Math.max(rightInfo.max, max);
min = Math.max(rightInfo.min, min);
allSize += rightInfo.allSize;
}
int p1 = -1;
if (leftInfo != null) {
//可能性1:左树的maxBSTSubTreeSize
p1 = leftInfo.maxBSTSubTreeSize;
}
int p2 = -1;
if (rightInfo != null) {
//可能性2
p2 = rightInfo.maxBSTSubTreeSize;
}
//可能性3:X为根
int p3 = -1;
//左树是否为BST
boolean leftBST = leftInfo == null ? true : (leftInfo.maxBSTSubTreeSize == leftInfo.allSize);
//右树是否为BST
boolean rightBST = rightInfo == null ? true : (rightInfo.maxBSTSubTreeSize == rightInfo.allSize);
if (leftBST && rightBST) {
//左树的max 是否 小于 x
boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.value);
//右树的min 是否 大于 x
boolean rightMinMoreX = rightInfo == null ? true : (rightInfo.min > x.value);
if (leftMaxLessX && rightMinMoreX) {
int leftSize = leftInfo == null ? 0 : leftInfo.allSize;
int rightSize = rightInfo == null ? 0 : rightInfo.allSize;
p3 = leftSize + rightSize + 1;
}
}
int maxBSTSubTreeSize = Math.max(Math.max(p1, p2), p3);
return new Info(maxBSTSubTreeSize, allSize, max, min);
}
}
边栏推荐
- Convolution和Batch normalization的融合
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- 万物并作,吾以观复|OceanBase 政企行业实践
- BBR 遭遇 CUBIC
- What can I do after buying a domain name?
- Intranet penetration | teach you how to conduct intranet penetration hand in hand
- Detailed explanation of 'viewpager' in compose | developer said · dtalk
- 公司里只有一个测试是什么体验?听听他们怎么说吧
- Catalogue of digital image processing experiments
- Brief introduction to common sense of Zhongtai
猜你喜欢
Talk about memory model and memory order
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Eight honors and eight disgraces of the programmer version~
Yolox enhanced feature extraction network panet analysis
购买完域名之后能干什么事儿?
抖音实战~点赞数量弹框
4 special cases! Schools in area a adopt the re examination score line in area B!
Writing of head and bottom components of non routing components
Three solutions to frequent sticking and no response of explorer in win11 system
C# MVC创建一个视图摆脱布局的影响
随机推荐
BBR encounters cubic
Go basic constant definition and use
golang入门:for...range修改切片中元素的值的另类方法
Fusion de la conversion et de la normalisation des lots
Use the scroll bar of souI when using the real window in souI
All things work together, and I will review oceanbase's practice in government and enterprise industry
Yolox enhanced feature extraction network panet analysis
(stinger) use pystinger Socks4 to go online and not go out of the network host
Submit code process
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
JDBC practice cases
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
内网渗透 | 手把手教你如何进行内网渗透
Agnosticism and practice makes perfect
Tiktok actual combat ~ number of likes pop-up box
Hisilicon VI access video process
Doorplate making C language
Temperature measurement and display of 51 single chip microcomputer [simulation]
【Redis笔记】压缩列表(ziplist)
Realize the linkage between bottomnavigationview and navigation