验证二叉搜索树

第一种:顺序存储。

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

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 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
豐儀麟阁贵1 小时前
基本数据类型
java·算法
乐迪信息3 小时前
乐迪信息:基于AI算法的煤矿作业人员安全规范智能监测与预警系统
大数据·人工智能·算法·安全·视觉检测·推荐算法
hsjkdhs4 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
立志成为大牛的小牛4 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
星空下的曙光4 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
StarPrayers.6 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
爱吃橘的橘猫6 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
235166 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
weixin_307779137 小时前
使用Python高效读取ZIP压缩文件中的UTF-8 JSON数据到Pandas和PySpark DataFrame
开发语言·python·算法·自动化·json