1043 Is It a Binary Search Tree (25)(25 分)
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
必须说,题目我纠结了好久才明白是什么意思
给出一个序列,利用这个数建立二叉搜索树
然后问 问这个序列是不是这个二叉搜索树的先序或者是这个二叉树镜像树的先序
如果是 那么就输出Yes 并且输出这个树的后序
如果不是的话就直接输出No
怎么说? 我先利用这个东西建立二叉搜索树...然后求出二叉搜索树的先序 判断是否和序列一样 若是一样则输出后序
或者判断是否和二叉搜索镜像树一样 若是一样则输出后序
二叉镜像搜索树的先序其实就是先根再右再左
后序就是先右再左再根 没有其他了 主要就是题目意思的问题............天呀我在想我会不会毁在读题目上
代码
#include<bits/stdc++.h>
using namespace std;
vector<int>pre,post,vec;
int n,x;
struct node
{
int data;
node *left,*right;
};
struct node *root = NULL;
void Insert(node * &root,int data)
{
if(root == NULL)
{
root = new node();
root->data = x;
root->left = root ->right =NULL;
}else if(data < root->data)
Insert(root->left,data);
else if(data >= root->data)
Insert(root->right,data);
}
void preorder1(node * root)
{
if(root)
{
pre.push_back(root->data);
preorder1(root->left);
preorder1(root->right);
}
}
void preorder2(node * root)
{
if(root)
{
pre.push_back(root->data);
preorder2(root->right);
preorder2(root->left);
}
}
void postorder1(node * root)
{
if(root)
{
postorder1(root->left);
postorder1(root->right);
post.push_back(root->data);
}
}
void postorder2(node * root)
{
if(root)
{
postorder2(root->right);
postorder2(root->left);
post.push_back(root->data);
}
}
int main()
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&x);
vec.push_back(x);
Insert(root,x);
}
pre.clear();
preorder1(root);
int i;
for(i = 0; i < n;i++ )
{
if(vec[i] != pre[i])
break;
}
if(i == n)
{
printf("YES\n");
postorder1(root);
for(int j = 0 ; j < n;j++)
printf("%d%c",post[j]," \n"[j==n-1]);
return 0;
}
pre.clear();
preorder2(root);
for(i = 0; i < n;i++ )
{
if(vec[i] != pre[i])
break;
}
if(i == n)
{
printf("YES\n");
postorder2(root);
for(int j = 0 ; j < n;j++)
printf("%d%c",post[j]," \n"[j==n-1]);
return 0;
}
printf("NO\n");
return 0;
}