浙江大学陈越教授数据结构PTA 题目——4-2 搜索树判断

一.问题描述:

        输入一个键值序列,判断该序列是否为某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,如果是,则输出对应二叉树的后序遍历序列。

二.思路:

        1.用数组存储输入的键值序列a,a[0]是二叉搜索树的根(Bintree BuildBST(int temp);),(用temp的原因是为了改变a[],因为后面还要用它  与  二叉搜索树和镜像二叉搜索树的先序遍历比较)再通过遍历数组的每下一个值,插入这个元素,递归创建二叉搜索树。

         2.分别先序遍历二叉搜索树、镜像二叉搜索树。并用数组保存其值(因为要与输入序列a[]做比较)

                镜像二叉搜索树:交换二叉搜索树每个节点的左子树和右子树。(与二叉搜索树唯一区别:遍历顺序从先左后右  变成  对二叉搜索树先右后左  的遍历。)

                        1)先序遍历:根、右子树、左子树。

                        2)后序遍历:右子树、左子树、根。

 

         3.若a[]与二叉搜索树、镜像二叉搜索树的先序遍历相同,则输出对应树的后序遍历。

三.代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 1111

typedef struct LNode *Position;//定义区别链表 
typedef Position Bintree; 
struct LNode{
	int Data;
	Bintree Left;
	Bintree Right; 
};

int a[MAXN]={0};

Bintree BuildBST(int temp)
{
	Bintree BST;
	BST = (Bintree)malloc(sizeof(struct LNode));
	BST->Data = temp;
	BST->Left = NULL;
	BST->Right = NULL;
	return BST;
}

void Insert(Bintree BST,int temp)	//创建二叉搜索树 
{
	if(BST->Data >temp){
		if(BST->Left ==NULL){
			Bintree P = (Bintree)malloc(sizeof(struct LNode));
			P->Data =temp;
			P->Left =NULL;
			P->Right =NULL;
			BST->Left = P;
		}
		else Insert(BST->Left ,temp);
	}
	else{
		if(BST->Right ==NULL){
			Bintree P = (Bintree)malloc(sizeof(struct LNode));
			P->Data =temp;
			P->Left =NULL;
			P->Right =NULL;
			BST->Right = P;
		}
		else Insert(BST->Right ,temp);
	}
}

int qushuPre[MAXN]={0};
int num_Preorder=0;
void Preorder(Bintree BST)
{
	if(BST!=NULL)
    {
        qushuPre[num_Preorder++]=BST->Data;
        Preorder(BST->Left);
        Preorder(BST->Right);
    }
}

int Mi_qushuPre[MAXN]={0};//镜像二叉树的先序排列 
int Mi_num_Preorder=0;
void Mi_Preorder(Bintree BST)
{
	if(BST!=NULL)
    {
        Mi_qushuPre[Mi_num_Preorder++]=BST->Data;
        Mi_Preorder(BST->Right);//因为镜像二叉树左右子树交换,所以要先遍历右子树 
        Mi_Preorder(BST->Left);
    }
}

int qushuPost[MAXN]={0};
int num_Postorder=0;
void Postorder(Bintree BST)
{
	if(BST!=NULL)
    {
        Postorder(BST->Left);
        Postorder(BST->Right);
        qushuPost[num_Postorder++]=BST->Data;
    }
}

int Mi_qushuPost[MAXN]={0};//镜像二叉树的后序排列 
int Mi_num_Postorder=0;
void Mi_Postorder(Bintree BST)
{
	if(BST!=NULL)
    {
        Mi_Postorder(BST->Right);//先遍历右子树 
        Mi_Postorder(BST->Left);
        Mi_qushuPost[Mi_num_Postorder++]=BST->Data;	
	}
}



int main()
{
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++) scanf("%d",&a[i]);
	int temp=a[0];
	Bintree BST = BuildBST(temp);
	for(i=1;i<n;i++) Insert(BST,a[i]);
	Preorder(BST);
	Mi_Preorder(BST);
	
	int flag = 1;//判断先序遍历是否为二叉搜索树 
	for(i=0;i<num_Preorder;i++){
		if(a[i]!=qushuPre[i]){
			flag = 0;
			break;
		}
	}
	if(flag){
		printf("YES\n");
		Postorder(BST);
		printf("%d",qushuPost[0]);
		for(i=1;i<num_Postorder;i++){
			printf(" %d",qushuPost[i]);
		}
		return 0;
	}
	
	flag = 1;//判断先序遍历是否为镜像二叉搜索树 
	for(i=0;i<Mi_num_Preorder;i++){
		if(a[i]!=Mi_qushuPre[i]){
			flag = 0;
			break;
		}
	}
	if(flag){
		printf("YES\n");
		Mi_Postorder(BST);
		printf("%d",Mi_qushuPost[0]);
		for(i=1;i<Mi_num_Postorder;i++){
			printf(" %d",Mi_qushuPost[i]);
		}
		return 0;
	}
	printf("NO\n");
	return 0;
}

  

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴同学·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值