当前位置:网站首页>Returns the size of the largest binary search subtree in a binary tree
Returns the size of the largest binary search subtree in a binary tree
2022-07-02 23:42:00 【Bright morning light】
1、 subject
Given the root node of a binary tree root, Returns the size of the largest binary search subtree in this binary tree
2、 analysis
A binary tree may not be a binary search tree , But its subtree may be a binary search tree .
Recursive routines using binary trees .
① The goal is : seek X The number of nodes of the maximum binary search subtree of the binary tree of the root node .
② possibility :
1)X Do not do the root of the situation :
(1) The result is on the left tree , On the left tree maxBSTSubTreeSize;
(2) The result is on the right tree , On the right tree maxBSTSubTreeSize;
2)X The situation of rooting . Then the whole tree is BST, It needs to be proved that the tree is BST, You need to get information from the left and right trees :(1) Is the left tree BST;
(2) Is the right tree BST;
(3) Left tree max < x;
(4) Right tree min > x;
(5) Left tree size + Right tree size + 1.
Sort out the union of all conditions to get the required information :
(1)maxBSTSubTreeSize;
(2) Is it BST
(3)max
(4)min
(5)size
But it can be simplified , If maxBSTSubTreeSize and size They are equal. , Then the whole tree is BST, therefore (2) This information can be omitted .
3、 Realization
C++ edition
// Verification has not been submitted yet
/************************************************************************* > File Name: 038. Returns the size of the largest binary search subtree of a binary tree .cpp > Author: Maureen > Mail: [email protected] > Created Time: 6、 ... and 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;// The maximum number of nodes to search a binary tree
int maxVal; // The maximum value of a tree
int minVal; // The minimum value of a tree
int allSize; // The number of nodes in the tree
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;
// possibility 1: The answer is in the left tree
p1 = leftInfo->maxBSTSubTreeSize;
}
if (rightInfo != nullptr) {
maxVal = max(maxVal, rightInfo->maxVal);
minVal = min(minVal, rightInfo->minVal);
allSize += rightInfo->allSize;
// possibility 2: The answer is in the right tree
p2 = rightInfo->maxBSTSubTreeSize;
}
// possibility 3: The whole tree is 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 edition
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; // The maximum number of nodes to search a binary tree
public int allSize; // The number of nodes in the tree
public int max; // The maximum value of a tree
public int min; // The minimum value of a tree
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; // node x In itself
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) {
// possibility 1: Left tree maxBSTSubTreeSize
p1 = leftInfo.maxBSTSubTreeSize;
}
int p2 = -1;
if (rightInfo != null) {
// possibility 2
p2 = rightInfo.maxBSTSubTreeSize;
}
// possibility 3:X Root
int p3 = -1;
// Whether the left tree is BST
boolean leftBST = leftInfo == null ? true : (leftInfo.maxBSTSubTreeSize == leftInfo.allSize);
// Whether the right tree is BST
boolean rightBST = rightInfo == null ? true : (rightInfo.maxBSTSubTreeSize == rightInfo.allSize);
if (leftBST && rightBST) {
// Left tree max whether Less than x
boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.value);
// Right tree min whether Greater than 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);
}
}
边栏推荐
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- 公司里只有一个测试是什么体验?听听他们怎么说吧
- (毒刺)利用Pystinger Socks4上线不出网主机
- Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
- Boost库链接错误解决方案
- 购买完域名之后能干什么事儿?
- Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
- 开发知识点
- JSON data transfer parameters
- Eight bit responder [51 single chip microcomputer]
猜你喜欢

【STL源码剖析】仿函数(待补充)

采用VNC Viewer方式远程连接树莓派

Flexible combination of applications is a false proposition that has existed for 40 years

Where is the win11 microphone test? Win11 method of testing microphone

Tiktok actual combat ~ number of likes pop-up box

Convolution和Batch normalization的融合

数据集-故障诊断:西储大学轴承的各项数据以及数据说明

Eight honors and eight disgraces of the programmer version~

面试过了,起薪16k

跨境电商如何通过打好数据底座,实现低成本稳步增长
随机推荐
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Sourcetree details
Bean加载控制
Convolution和Batch normalization的融合
基于Pyqt5工具栏按钮可实现界面切换-1
Leetcode relaxation question - day of the week
VIM interval deletion note
Yolox enhanced feature extraction network panet analysis
MarkDown基本语法
开发知识点
Remote connection of raspberry pie by VNC viewer
The difference between new and make in golang
MySQL Foundation
Solution to boost library link error
返回二叉树两个节点间的最大距离
实用系列丨免费可商用视频素材库
What experience is there only one test in the company? Listen to what they say
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
How to apply for company email when registering in company email format?
How much do you know about synchronized?