题目
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.
输入
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.
输出
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, orNO
if not. Then if the answer isYES
, 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.
样例输入
样例输入1:
7
8 6 5 7 10 8 11样例输入2:
7
8 10 11 8 6 7 5样例输入3:
7
8 6 8 5 10 9 11
样例输出
样例输出1:
YES
5 7 6 8 11 10 8样例输出2:
YES
11 8 10 7 5 6 8样例输出3:
NO
题意理解
给你一个二叉排序树的先序序列让你求 是否是二叉排序树的先序序列或者是镜像先序序列
如果是先序序列 那么输出后序遍历的序列
如果是镜像先序序列 那么输出镜像后序遍历的序列
众所周知 二叉排序树树是比自身小的节点就放入左孩子节点 比自身大的节点就放入右孩子节点
这题题目已经给你规定好了 如果节点值小于自身节点那么放入左孩子节点 大于等于的都放入右孩子节点
那么各种序列的遍历方式是什么你
- 先序遍历 先根节点 再左孩子 再右孩子 (根左右)
- 镜像先序遍历 先根节点 再右孩子 再左孩子 (根右左)
- 后序遍历 先左孩子 再右孩子 最后根节点 (左右根)
- 后序镜像遍历 先右孩子 再左孩子 最后根节点 (右左根)
现在知道了四种遍历序列了以后 我们直接建树 然后根据情况去输出遍历的序列即可
注意条件不要看错即可
镜像先序遍历结果和原来给的序列相同的话 要输出的是后序镜像遍历
先序遍历和原来给的序列相同的话 要输出的是后序遍历
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL ;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
#define fx first
#define fy second
#define io ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int INF=0x3f3f3f3f;
const double DINF=0x7f7f7f7f;
const LL LINF=1e18;
const int N=2e5+10;
const int M=2e2+10;
const int MOD=1e9+7;
typedef struct Node{
int data;
Node *l,*r;
}Node;
vector<int>a,pre,mirror_pre,post,mirror_post;
void BST_insert(Node *&T,int x){
if(T==NULL){
T=(Node *)malloc(sizeof(Node));
T->data=x;
T->l=T->r=NULL;
return;
}
if(x<T->data)BST_insert(T->l,x);
else BST_insert(T->r,x);
}
Node *create_tree(){
Node *root=NULL;
for(int i=0;i<a.size();i++){
int x=a[i];
BST_insert(root,x);
}
return root;
}
void visit_pre(Node *T){
if(T==NULL)return;
pre.push_back(T->data);
visit_pre(T->l);
visit_pre(T->r);
}
void visit_mirror_pre(Node *T){
if(T==NULL)return;
mirror_pre.push_back(T->data);
visit_mirror_pre(T->r);
visit_mirror_pre(T->l);
}
void visit_post(Node *T){
if(T==NULL)return;
visit_post(T->l);
visit_post(T->r);
post.push_back(T->data);
}
void visit_mirror_post(Node *T){
if(T==NULL)return;
visit_mirror_post(T->r);
visit_mirror_post(T->l);
mirror_post.push_back(T->data);
}
int main(){
int n,x;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&x);
a.push_back(x);
}
Node *T=create_tree();
visit_pre(T);
visit_mirror_pre(T);
if(mirror_pre==a){
puts("YES");
visit_mirror_post(T);
for(int i=0;i<mirror_post.size();i++){
if(i)printf(" ");
printf("%d",mirror_post[i]);
}
puts("");
}
else if(pre==a){
puts("YES");
visit_post(T);
for(int i=0;i<post.size();i++){
if(i)printf(" ");
printf("%d",post[i]);
}
puts("");
}
else {
puts("NO");
}
free(T);
return 0;
}