给定一个序列,判断它是否是一棵二叉搜索树的先(后)序遍历序列

19年复旦大学研究生初试的一道数据结构题。可以根据二叉搜索树的特点以及递归思路解答:该序列递归地满足一部分为 左子树的值(均小于根节点),一部分为右子树的值(均大于根节点)以及根节点,这样只需找到左右子树的分界点作递归判断即可。

#include<iostream>
using namespace std;
const int maxn=100;
bool Ispostorder(int Post[],int length)//为了简化方便,直接使用数组名代替数组首地址 
{
	if(Post==NULL||length<=0)
	return false;
	
	int root=Post[length-1];
	int i=0;
	for(;i<length-1;++i)
	if(Post[i]>root)
	break;
	
	int j=i;
	for(;j<length-1;++j)
	if(Post[j]<root)
	return false;
	
	bool left=true;
	if(i>0)
	left=Ispostorder(Post,i);
	
	bool right=true;
	if(i<length-1)
	right=Ispostorder(Post+i,length-i-1);//最后一个节点是根 
	return (left&&right);
}
int main()
{
	cout<<"please input the postorder of the array"<<endl;
	int length;
	int Post[maxn]; 
	cin>>length;
	for(int i=0;i<length;i++)
	cin>>Post[i];
	
    if(Ispostorder(Post,length))
    cout<<"true"<<endl;
    else
    cout<<"false"<<endl;
}

这题还可以引申为已知后(先)序遍历序列和中序遍历序列求出先(后)序遍历序列,也可以理解为中序序列加上另一种遍历序列来重建二叉树,同样是以递归思路解决。

//利用中序序列和先序序列创建二叉树 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
int maxsize;
int n;
const int MAXN=100;
typedef struct BTNode{
    int data;
    struct BTNode *lchild;
    struct BTNode *rchild;
}BTNode,*BiTree; 
//先序+中序
BiTree pre_in_create(int Preorder[],int Inorder[],int preL,int preR,int inL,int inR){
	    int Llen,Rlen;
        BiTree bt=new (BTNode); 
        bt->data=Preorder[preL];
		for(int i=inL;i<=inR;i++)
		if(Inorder[i]==Preorder[preL]){
		Llen=i-inL;
		Rlen=inR-i; 
		break;
	}
	if(Llen)
	bt->lchild=pre_in_create(Preorder,Inorder,preL+1,preL+Llen,inL,inL+Llen-1);
	else
	bt->lchild=NULL;
	if(Rlen) 
	bt->rchild=pre_in_create(Preorder,Inorder,preR-Rlen+1,preR,inR-Rlen+1,inR);
	else
	bt->rchild=NULL;
	return bt;
} 
//后序+中序
BiTree post_in_create(int Postorder[],int Inorder[],int postL,int postR,int inL,int inR){
	    int Llen,Rlen;
        BiTree bt=new (BTNode); 
        bt->data=Postorder[postR];
		for(int i=inL;i<=inR;i++)
		if(Inorder[i]==Postorder[postR]){
		Llen=i-inL;
		Rlen=inR-i; 
		break;
	}
	if(Llen)
	bt->lchild=post_in_create(Postorder,Inorder,postL,postL+Llen-1,inL,inL+Llen-1);
	else
	bt->lchild=NULL;
	if(Rlen) 
	bt->rchild=post_in_create(Postorder,Inorder,postR-Rlen,postR-1,inR-Rlen+1,inR);
	else
	bt->rchild=NULL;
	return bt;
} 
BiTree create(){//这是采用先序创建二叉树 
    BiTree bt;
    int  da;
    cin>>da;
    if(da==0)
    bt=NULL;
    else if(da>0){
        bt=new (BTNode);
        bt->data=da; 
        bt->lchild=create();
        bt->rchild=create();
    }
    else
    cout<<"error input"<<endl;
        return bt;
    }     
void pre(BiTree bt){
        if(bt!=NULL){
            cout<<bt->data<<" ";
            pre(bt->lchild);
            pre(bt->rchild);
          }
      }        

int main(){
	cout<<"请输入节点的个数:"<<endl;
	cin>>n;
	
	int Preorder[maxsize];
	int Inorder[maxsize]; 
	
	cout<<"请输入二叉树的前序序列: "<<endl;
	for(int i=1;i<=n;i++)
	cin>>Preorder[i];
	cout<<"请输入二叉树的中序序列: "<<endl;
	for(int i=1;i<=n;i++)
	cin>>Inorder[i];
	
	BiTree root;
	root=pre_in_create(Preorder,Inorder,1,n,1,n); 
	pre(root);
}     

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值