验证二叉搜索树

第一种:顺序存储。

算法思想:根据二叉搜索树的性质:左边的值小于根的值,右边的值大于根的值。根据判断性质是否满足判断是否是二叉搜索树。

cpp 复制代码
#include<iostream>
using namespace std;
const int maxsize=1000;
typedef struct BinaryTree{
	int sqnode[maxsize];
	int element;
}BinaryTree;
bool isBinary(BinaryTree& tree){
	if(tree.element==0) return true;
	int *a=tree.sqnode;
	for(int i=0;i<tree.element;i++){
		if(2*i+1<tree.element&&a[2*i+1]!=-1&&a[2*i+1]>=a[i]) return false;//还得判断元素的大小是否满足
		if(2*i+2<tree.element&&a[2*i+2]!=-1&&a[2*i+2]<=a[i]) return false;
	}
	return true;
}
int main(){
	BinaryTree t;
	t.element=5;
	t.sqnode[0]=4;
	t.sqnode[1]=2;
	t.sqnode[2]=7;
	t.sqnode[3]=1;
	t.sqnode[4]=3;
	if(isBinary(t)) {cout<<"1";}
	else {cout<<"9";}
	
}

这里需要注意如果双亲下标为(i),左右孩子本来应该是(2i)和(2i+1),但是数组的下标是从0开始,而数组下标的这样的写法默认从下标1开始,所以在判断的时候应该对孩子的下标加1,满足要求。

判断条件:1、数组的长度要大于孩子所在的下标,这样元素才存在。2、当不为空且不满足左孩子小于双亲,右孩子大于双亲的时候返回false,其他时候返回true,这样就完成了对二叉搜索树的判断。

第二种:链式存储。

算法思想:二叉搜索树的中序遍历结果是递增序列,根据判断是否递增得到是否是二叉搜索树。

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;

typedef struct BinaryTree{
	int val;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

vector<int> vec;
void traversal(BinaryTree *root){
	if(root==NULL) return;
	traversal(root->left);
	vec.push_back(root->val);
	traversal(root->right);
}

bool isBinary(BinaryTree *root){
	vec.clear();
	traversal(root);
	for(int i=1;i<vec.size();i++){
		if(vec[i]<vec[i-1]) return false;
	}
	return true;
}
int main(){
	BinaryTree *t=new BinaryTree();
	t->val=4;
	t->left=new BinaryTree();
	t->left->val=2;
	t->right=new BinaryTree();
	t->right->val=7;
	t->left->left=new BinaryTree();
	t->left->left->val=1;
	t->left->right=new BinaryTree();
	t->left->right->val=3;
	
	if(isBinary(t)) {cout<<"1";}
	else {cout<<"9";}
	
}

先在void traversal()函数里得到中序遍历的结果,再到bool函数中判断是否为递增序列。

当然了,除了得到递增序列之外,我们还可以在遍历的过程中就判断是否满足递增。

cpp 复制代码
#include<iostream>
using namespace std;

typedef struct BinaryTree{
	int val;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

long long maxval=LONG_MIN;

bool isBinary(BinaryTree *root){
	if(root==NULL) return true;
	bool left=isBinary(root->left);
	if(maxval<root->val) maxval=root->val;
	else return false;
	bool right=isBinary(root->right);
	return left&&right;
}
int main(){
	BinaryTree *t=new BinaryTree();
	t->val=4;
	t->left=new BinaryTree();
	t->left->val=2;
	t->right=new BinaryTree();
	t->right->val=7;
	t->left->left=new BinaryTree();
	t->left->left->val=1;
	t->left->right=new BinaryTree();
	t->left->right->val=3;
	
	if(isBinary(t)) {cout<<"1";}
	else {cout<<"9";}
	
}

引入maxval这个参数,按照 左中右 判断当前节点的值是否大于maxval的值,如果大于则更新maxval(这是必然的,因为节点值通常是合理的数值,肯定大于这个极小值)

Long_Min这个参量只是一个进入作用,他让我们直接开始比较每一个节点的值是否是满足递增这样的关系。long int类型的最小值,通常为 - 9223372036854775808

如果编译器没有给出LONG_MIN这个值我们该怎么做?

cpp 复制代码
#include<iostream>
using namespace std;

typedef struct BinaryTree{
	int val;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

BinaryTree*pre=NULL;

bool isBinary(BinaryTree *root){
	if(root==NULL) return true;
	bool left=isBinary(root->left);
	
	if(pre!=NULL && pre->val>=root->val) return false;
	pre=root;
	
	bool right=isBinary(root->right);
	return left&&right;
}
int main(){
	BinaryTree *t=new BinaryTree();
	t->val=4;
	t->left=new BinaryTree();
	t->left->val=2;
	t->right=new BinaryTree();
	t->right->val=7;
	t->left->left=new BinaryTree();
	t->left->left->val=1;
	t->left->right=new BinaryTree();
	t->left->right->val=3;
	
	if(isBinary(t)) {cout<<"1";}
	else {cout<<"9";}
	
}

记录前一个结点,在中序遍历的过程中实现比较。

最后是迭代的方式判断是否为二叉搜索树:

cpp 复制代码
#include<iostream>
#include<stack>
using namespace std;

typedef struct BinaryTree{
	int val;
	struct BinaryTree *left;
	struct BinaryTree *right;
}BinaryTree;

bool isBinary(BinaryTree *root){
	stack<BinaryTree*> st;
	BinaryTree *cur=root;
	BinaryTree *pre=NULL;
	while(cur!=NULL||!st.empty()){
		if(cur!=NULL){
			st.push(cur);
			cur=cur->left;
		}
		else{
			cur=st.top();
			st.pop();
			if(pre!=NULL&&cur->val<=pre->val) return false;
			pre=cur;
			cur=cur->right;
		}
	}
	return true;
}
int main(){
	BinaryTree *t=new BinaryTree();
	t->val=4;
	t->left=new BinaryTree();
	t->left->val=2;
	t->right=new BinaryTree();
	t->right->val=7;
	t->left->left=new BinaryTree();
	t->left->left->val=1;
	t->left->right=new BinaryTree();
	t->left->right->val=3;
	
	if(isBinary(t)) {cout<<"1";}
	else {cout<<"9";}
	
}
相关推荐
醉颜凉1 分钟前
区块链不可篡改机制全解析:从哈希锁链到51%攻击
算法·区块链·哈希算法
我是伪码农14 分钟前
12.5程序设计
数据结构·算法
Shaoshing24 分钟前
LeetCode100 152.乘积最大子数组
数据结构·算法·leetcode
吞下星星的少年·-·28 分钟前
力扣 162. 寻找峰值(枚举,二分查找)
算法·leetcode·枚举·二分
风落青芜35 分钟前
C++模板初阶:函数模板与类模板
开发语言·数据结构·c++·算法
纪念 22944 分钟前
C++ 算法模板与错题 (枚举)
c++·redis·算法
吞下星星的少年·-·10 小时前
U549625 小苯的区间和疑惑(枚举,前后缀)
算法
李剑一10 小时前
到底啥是千年虫2.0?为什么量子计算能够突破公钥私钥体系?什么是后量子密码?
算法·ai编程
Techblog of HaoWANG10 小时前
具身智能机器人专题(7)大模型 / 语音 / RAG 的容器化架构与系统开发
人工智能·算法·大模型·边缘计算·具身智能
专注仿真10 小时前
仿真智能体训练(评估指标篇):探测/杀伤链时延算法详解与实战
算法·afsim智能体·afsim智能体训练·评估算法·探测时延算法·杀伤链时延算法