1043 Is It a Binary Search Tree (25 point(s))

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

经验总结:

这一题.....其实就是,根据所给的先序序列构建一个二叉搜索树,然后对这个二叉搜索树进行先序遍历,如果所得遍历序列与题目所给的初始遍历序列完全相同,那么就是一个BST,否则不是,当然,建立可以只建立一种树,利用遍历方法的不同,就可以实现镜像BST与BST的先序遍历,最后再后序遍历之,输出结果。
当然,还有一种更高级的方法,利用遍历方式之间的联系,直接根据所给的先序序列构建对应的后序序列,如果后续序列的大小与先序不同,则不是BST,反之则可以直接输出后序的序列,这种方法在速度和空间上都快一些,只是理解起来有点麻烦,思路通了就很方便啦~(๑•̀ㅂ•́)و✧

AC代码

第一种

#include <cstdio>
#include <vector>
using namespace std;
bool flag=false;
vector<int> post,pre,temp;
struct node
{
int data;
node * lchild,* rchild;
};
void insert(node * &root,int data)
{
if(root==NULL)
{
root=new node;
root->data=data;
root->lchild=root->rchild=NULL;
return ;
}
if(data<root->data)
insert(root->lchild,data);
else
insert(root->rchild,data);
}
void postorder(node * root)
{
if(root==NULL)
return ;
if(flag)
{
postorder(root->rchild);
postorder(root->lchild);
}
else
{
postorder(root->lchild);
postorder(root->rchild);
}
post.push_back(root->data);
}
void preorder(node * root)
{
if(root==NULL)
return ;
temp.push_back(root->data);
if(flag)
{
preorder(root->rchild);
preorder(root->lchild);
}
else
{
preorder(root->lchild);
preorder(root->rchild);
}
}
int main()
{
int n;
scanf("%d",&n);
pre.resize(n);
node * root=NULL;
for(int i=0;i<n;++i)
{
scanf("%d",&pre[i]);
insert(root,pre[i]);
}
preorder(root);
if(temp!=pre)
{
temp.clear();
flag=true;
preorder(root);
}
if(pre==temp)
{
postorder(root);
printf("YES\n");
for(int i=0;i<n;++i)
printf("%d%c",post[i],i<n-1?' ':'\n');
}
else
printf("NO\n");
return 0;
}

第二种

#include <cstdio>
#include <vector>
using namespace std;
bool flag=false;
vector<int> post,pre;
void getPost(int left,int right)
{
int i=left+1,j=right;
if(flag)
{
while(i<=right&&pre[i]>=pre[left])
++i;
while(j>left&&pre[j]<pre[left])
--j;
}
else
{
while(i<=right&&pre[i]<pre[left])
++i;
while(j>left&&pre[j]>=pre[left])
--j;
}
if(i-j!=1)
return ;
getPost(left+1,j);
getPost(i,right);
post.push_back(pre[left]);
}

int main()
{
int n;
scanf("%d",&n);
pre.resize(n);
for(int i=0;i<n;++i)
scanf("%d",&pre[i]);
getPost(0,n-1);
if(post.size()!=n)
{
post.clear();
flag=true;
getPost(0,n-1);
}
if(post.size()==n)
{
printf("YES\n");
for(int i=0;i<n;++i)
printf("%d%c",post[i],i<n-1?' ':'\n');
}
else
printf("NO\n");
return 0;
}