验证二叉搜索树

第一种:顺序存储。

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

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";}
	
}
相关推荐
老赵聊算法、大模型备案30 分钟前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
CoderYanger1 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
厕所博士2 小时前
红黑树原理前置理解—— 2-3 树
算法·2-3树·红黑树原理理解前置
萌>__<新2 小时前
力扣打卡每日一题————除自身外所有元素的乘积
数据结构·算法
xu_yule3 小时前
算法基础—搜索(2)【记忆化搜索+BFS+01BFS+Floodfill]
数据结构·算法
s09071363 小时前
Xilinx FPGA使用 FIR IP 核做匹配滤波时如何减少DSP使用量
算法·fpga开发·xilinx·ip core·fir滤波
老马啸西风3 小时前
成熟企业级技术平台-10-跳板机 / 堡垒机(Bastion Host)详解
人工智能·深度学习·算法·职场和发展
子夜江寒3 小时前
逻辑回归简介
算法·机器学习·逻辑回归
软件算法开发3 小时前
基于ACO蚁群优化算法的多车辆含时间窗VRPTW问题求解matlab仿真
算法·matlab·aco·vrptw·蚁群优化·多车辆·时间窗
another heaven3 小时前
【软考 磁盘磁道访问时间】总容量等相关案例题型
linux·网络·算法·磁盘·磁道