验证二叉搜索树

第一种:顺序存储。

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

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";}
	
}
相关推荐
2601_967760785 小时前
2026年PDF压缩与页码添加工具技术实测:性能、算法与本地化适配深度对比
算法·pdf
不会就选b5 小时前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode
moonrailgun6 小时前
用 Node.js 复刻 Codex Astra 的终端星光
前端·javascript·算法
罗西的思考6 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(1)--- 总体
人工智能·算法·机器学习
lvwangshu7 小时前
图论:LCA、树的直径、树的重心、二分图与 Tarjan 缩点
算法·图论
302wanger8 小时前
干与湿:AI 拿走脑力之后,人剩下什么
算法
计算机编程-吉哥9 小时前
脑肿瘤MRI智能识别系统:基于深度学习的像素级脑肿瘤语义分割平台【计算机毕业设计选题推荐】
人工智能·python·深度学习·算法·毕业设计·课程设计·大数据毕业设计选题推荐
用户204937554959 小时前
端侧语音部署踩坑:模型能跑不等于终端真的能用
后端·算法
shehuiyuelaiyuehao9 小时前
算法39,位运算,消失的两个数字
java·数据结构·算法
ZiLing9 小时前
2026 ROS 2 Lyrical 踩坑实录(一):编译与依赖——rosdep、现代 CMake 与 CMake 4.x
算法