验证二叉搜索树

第一种:顺序存储。

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

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";}
	
}
相关推荐
知来者逆20 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊24 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北25 分钟前
力扣-160.相交链表
算法·leetcode·链表
爱研究的小陈1 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing1 小时前
二叉树的最大宽度计算
算法·面试
BB_CC_DD2 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘3 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV3 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8244 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify4 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法