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
-----------------------------------这是题目和解题的分割线-----------------------------------
我起初还纳闷,才给了先序序列要怎么建树啊= = 别忘了二叉搜索树的性质啊喂!!!
这道题在输入的时候建立好树和镜像树,并保存这时候的输入顺序,再对它们进行先序遍历,对比顺序是否一致。
#include<cstdio>
#include<vector>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
vector<int> tmp,order;
int num = 0,n;
//根据搜索树的性质建树
node* insertTr(node *t,int x)
{
if(t==NULL) //为空,即要插入的地方
{
t = new node; //申请内存
t->data = x;
t->left = NULL;
t->right = NULL;
}
else if(x>=t->data) t->right = insertTr(t->right,x);
else t->left = insertTr(t->left,x);
return t;
}
//建立镜像树
node* insertMi(node *t,int x)
{
if(t==NULL)
{
t = new node;
t->data = x;
t->left = NULL;
t->right = NULL;
}
else if(x<t->data) t->right = insertMi(t->right,x);
else t->left = insertMi(t->left,x);
return t;
}
//先序遍历
void preOrder(node* t)
{
if(t)
{
tmp.push_back(t->data); //存储先序遍历的顺序
preOrder(t->left);
preOrder(t->right);
}
}
//后序遍历
void postOrder(node* t)
{
if(t)
{
postOrder(t->left);
postOrder(t->right);
num++;
printf("%d",t->data);
if(num!=n) printf(" "); //末尾无空格
}
}
int main()
{
int x,i;
//一开始要指向NULL,这样才能进入建树函数的if(NULL)
node *tree = NULL,*Mitree = NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
order.push_back(x); //存储题目给出的先序遍历
tree = insertTr(tree,x); //建树
Mitree = insertMi(Mitree,x); //建镜像树
}
preOrder(tree);
for(i=0;i<n;i++)
if(tmp[i]!=order[i]) break;
if(i==n) //如果该树的先序遍历和题目给出的一致
{
printf("YES\n");
postOrder(tree);
}
else //否则对镜像树先序遍历
{
tmp.clear(); //先要清空之前存储的数据
preOrder(Mitree);
for(i=0;i<n;i++)
{
if(tmp[i]!=order[i])
{
printf("NO\n"); //如果还是不相等,直接NO
return 0;
}
}
printf("YES\n");
postOrder(Mitree);
}
return 0;
}