PAT甲级1043. Is It a Binary Search Tree(BST)

1043. Is It a Binary Search Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO

【分析】二叉搜索树相关操作(建立&遍历&镜像)

        题意:建立二叉搜索树,然后进行先序遍历。

        (1)若先序遍历序列与输入序列相同,则输出"YES"并输出该树的后序遍历序列;

        (2)若先序遍历序列与输入序列不同,则建立上述二叉搜索树的镜像树(左右子树"互换"),然后进行先序遍历。若先序遍历序列与输入序列相同,则输出"YES"并输出该镜像树的后序遍历序列;

        (3)否则,输出"NO"。

        此题的关键在于左右子树的“互换”操作。此外,对最先建立出的二叉搜索树,其按照"右子树->左子树->根节点"的顺序遍历即镜像树的先序遍历序列。

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn=1010;
int N,a[maxn];
int cnt=0,pre[maxn];   //保存BST或镜像BST的先序遍历序列 
int first=1;
int suc=1;    //记录BST或镜像BST的先序遍历序列是否与输入序列相同 1BST 0镜像BST -1不同 
struct BiTree 
{
	int data;
	struct BiTree *left;
	struct BiTree *right;
} *bt;
//创建二叉搜索树 
void createBST(struct BiTree *&b,int num)
{
	if(b==NULL)
	{
		b=(BiTree *)malloc(sizeof(BiTree));
		b->data=num;
		b->left=NULL;
		b->right=NULL;
		return;
	}
	else
	{
		if(num<b->data)
			createBST(b->left,num);
		else
			createBST(b->right,num);
	}
}
//左右子树"互换",得到镜像BST 
void swapBST(struct BiTree *b)
{
	BiTree *temp;
	if(b==NULL)
		return;
	//换节点 
	temp=b->left;
	b->left=b->right;
	b->right=temp;
	//换左右子树 
	swapBST(b->left);
	swapBST(b->right);
}
//先序遍历BST,同时保存先序遍历序列 
void preOrderBST(struct BiTree *b)
{
	if(b!=NULL)
	{
		pre[cnt++]=b->data;
		preOrderBST(b->left);
		preOrderBST(b->right);
	}
}
//输出BST的后序遍历序列 
void postOrderBST(struct BiTree *b)
{
	if(b!=NULL)
	{
		postOrderBST(b->left);
		postOrderBST(b->right);
		if(first==1)
		{
			printf("%d",b->data);
			first=0;
		}
		else
			printf(" %d",b->data);
	}
}
int main()
{
	int i;
	scanf("%d",&N);
	for(i=0;i<N;i++)
	{
		scanf("%d",&a[i]);
		createBST(bt,a[i]);
	}
	preOrderBST(bt); 
	for(i=0;i<N;i++)
	{
		if(a[i]!=pre[i])
		{
			suc=0;
			break;
		}
	}
	//BST的先序遍历序列与输入序列相同 
	if(suc==1)
	{
		printf("YES\n");
		postOrderBST(bt);
		printf("\n");
	}
	else
	{
		cnt=0;
		first=1;
		swapBST(bt);
		preOrderBST(bt);
		for(i=0;i<N;i++)
		{
			if(a[i]!=pre[i])
			{
				suc=-1;
				break;
			}
		}
		//镜像BST的先序遍历序列与输入序列不同 
		if(suc==-1)
			printf("NO\n");
		//镜像BST的先序遍历序列与输入序列相同 
		else
		{
			printf("YES\n");
			postOrderBST(bt);
			printf("\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值