题目来源:PAT (Advanced Level) Practice
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
words:
recursively 递归地 Binary Search Tree 二叉排序树 properties 特性,性质
思路:
1. 首先根据给定的结点个数n和前序遍历序列pre[]来创建二叉搜索树;
2. 对创建的树进行前序遍历,得到树的前序遍历序列pre1[],若该序列和题目给定的序列pre[]一致,则说明该树是二叉排序树,那么输出“YES”和对其进行后序遍历并输出即可结束;
3. 若该序列和题目给定的序列pre[]不一致,则对创建的树的镜像进行前序遍历,得到镜像的前序遍历序列pre2[],若该序列和题目给定的序列pre[]一致,则说明该树是二叉排序树的镜像,那么输出“YES”和对其进行后序遍历并输出即可结束;
4. 否则说明该树既不是二叉排序树也不是二叉排序树的镜像,那么输出“NO”即可结束;
//PAT ad 1043 Is It a Binary Search Tree
#include <iostream>
using namespace std;
#define N 1005
int n; //结点数
int pre[N],pre1[N],pre2[N]; //前序遍历数组
int post[N]; //后序遍历结果
typedef struct bt //二叉树数据结构
{
int data;
struct bt* Lchild;
struct bt* Rchild;
}TREE,*LPtree;
LPtree node(int data) //创建结点
{
LPtree newNode=new TREE;
newNode->data=data;
newNode->Lchild=NULL;
newNode->Rchild=NULL;
return newNode;
}
int k=0;
void createTree(LPtree &T) //创建二叉搜索树
{
T=node(pre[k++]); //创建根结点
LPtree p=T;
while(k<n)
{
if(pre[k]<p->data) //小于
{
if(p->Lchild) //左非空
p=p->Lchild;
else
{
p->Lchild=node(pre[k++]);p=T;
}
}
else //大于等于
{
if(p->Rchild) //右非空
p=p->Rchild;
else
{
p->Rchild=node(pre[k++]);p=T;
}
}
}
}
void postOrder1(LPtree T) //后序遍历
{
if(T)
{
postOrder1(T->Lchild);
postOrder1(T->Rchild);
post[k++]=T->data;
}
}
void postOrder2(LPtree T) //后序遍历树的镜像
{
if(T)
{
postOrder2(T->Rchild);
postOrder2(T->Lchild);
post[k++]=T->data;
}
}
void preOrder1(LPtree T) //前序遍历
{
if(T)
{
pre1[k++]=T->data;
preOrder1(T->Lchild);
preOrder1(T->Rchild);
}
}
void preOrder2(LPtree T) //前序遍历树的镜像
{
if(T)
{
pre2[k++]=T->data;
preOrder2(T->Rchild);
preOrder2(T->Lchild);
}
}
void print(int *a) //打印数组a
{
int i=0;
for(i=0;i<n-1;i++)
cout<<a[i]<<" ";
cout<<a[i]<<endl;
}
bool equal(int *a,int *b) //判断两个数组a,b中元素是否相等
{
for(int i=0;i<n;i++)
if(a[i]!=b[i])
return false;
return true;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++) //输入前序序列
cin>>pre[i];
LPtree T;
createTree(T); //创建树
k=0;preOrder1(T); //前序遍历树
k=0;preOrder2(T); //前序遍历树的镜像
if(equal(pre,pre1))
{
cout<<"YES"<<endl; //是二叉排序树
k=0;postOrder1(T);
print(post);
}
else if(equal(pre,pre2))
{
cout<<"YES"<<endl; //是二叉排序树的镜像
k=0;postOrder2(T);
print(post);
}
else
cout<<"NO"<<endl; //不是二叉排序树或镜像
return 0;
}