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
解题思路:直接建树,建两种树,每一种树建一遍,然后先序遍历,是否与给出的相同,若是相同就输出YES,若不是就输出NO,如果不会建二叉搜索树,可以看看这题,这题应该比较容易懂https://blog.csdn.net/Imagirl1/article/details/87983686,建树中,要注意返回的是node类型的,所以递归调用函数的时候,应该是递归调用所返回的值赋值给一个变量。
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
struct node{
int v;
struct node *left,*right;
};
vector<int>pre,ispre,post,in;
node *build(node *root,int v)
{
if(root==NULL)
{
root=new node();
root->v=v;
root->left=NULL;
root->right=NULL;
}
else if(v<root->v) root->left=build(root->left,v);
else root->right=build(root->right,v);
return root;
}
node *mirror(node*root,int v)
{
if(root==NULL)
{
root=new node();
root->v=v;
root->left=NULL;
root->right=NULL;
}
else if(v>=root->v) root->left=mirror(root->left,v);
else root->right=mirror(root->right,v);
return root;
}
void preorder(node *root )
{
if(root==NULL) return;
pre.push_back(root->v);
preorder(root->left);
preorder(root->right);
}
void postorder(node*root)
{
if(root==NULL) return;
postorder(root->left);
postorder(root->right);
post.push_back(root->v);
}
void inorder(node*root)
{
if(root==NULL) return;
inorder(root->left);
printf("%d ",root->v);
//in.push_back(root->v);
inorder(root->right);
}
int main(void)
{
int n;
bool isbst=true,ismirror=true;
scanf("%d",&n);
ispre.resize(n);
node *root=NULL,*mroot=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&ispre[i]);
root=build(root,ispre[i]);
mroot=mirror(mroot,ispre[i]);
}
preorder(root);
for(int i=0;i<n;i++)
{
if(pre[i]!=ispre[i]){
isbst=false;
break;
}
}
if(isbst){
printf("YES\n");
postorder(root);
for(int i=0;i<n;i++)
{
printf("%d",post[i]);
if(i!=n-1) printf(" ");
}
return 0;
}else{
pre.clear();
preorder(mroot);
for(int i=0;i<n;i++)
{
if(pre[i]!=ispre[i]){
ismirror=false;
break;
}
}
if(ismirror)
{
printf("YES\n");
postorder(mroot);
for(int i=0;i<n;i++)
{
printf("%d",post[i]);
if(i!=n-1) printf(" ");
}
return 0;
}
}
printf("NO");
return 0;
}