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或镜像BST,若可以,则写出其后序遍历序列。
思路
本题难点在于镜像BST的处理。先利用先序遍历序列构造一棵BST(注意这里题目所给先序遍历序列只是为了构造BST,并不一定是该BST的先序遍历序列),然后对该BST进行先序遍历:
- 该BST的先序遍历序列与原序列相同,则原序列能构造出BST,输出该BST的后序遍历序列。
- 该BST的先序遍历序列与所给序列不同,则对该BST进行镜像先序遍历(即先访问右子树,再访问左子树),若所得序列与原序列相同,则原序列能构造出镜像BST,输出镜像后序遍历序列。
- 该BST先序遍历结果、镜像先序遍历结果均与原序列不同,则输出“NO"。
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct node{
int data;
node *lchild, *rchild;
}*BTree, BNode;
int n, data[1001];
void insert(BTree &BT, int x){ //BST结点的插入
if(BT == NULL){
BT = new node;
BT->lchild = NULL;
BT->rchild = NULL;
BT->data = x;
return;
}
if(x < BT->data)
insert(BT->lchild, x);
else
insert(BT->rchild, x);
}
void create(BTree &BT){ //建立BST
for(int i = 0; i < n; i++)
insert(BT, data[i]);
}
int cnt = 0, cnt2 = 0;
void print(BTree &BT){ //BST后序遍历
if(BT == NULL) return;
print(BT->lchild);
print(BT->rchild);
cnt++;
if(cnt != 1) cout<<" ";
cout<<BT->data;
}
void print_mirror(BTree &BT){ //镜像BST后序遍历
if(BT == NULL) return;
print_mirror(BT->rchild);
print_mirror(BT->lchild);
cnt2++;
if(cnt2 != 1) cout<<" ";
cout<<BT->data;
}
int i = 0, j = 0;
//对BST进行先序遍历,并判断所得结果是否与所给序列一致
bool judge(BTree &BT){
if(BT == NULL) return true;
if(BT->data != data[i++])
return false;
return judge(BT->lchild) && judge(BT->rchild);
}
//对镜像BST进行先序遍历,并判断所得结果是否与所给序列一致
bool judge_mirror(BTree &BT){
if(BT == NULL) return true;
if(BT->data != data[j++])
return false;
return judge_mirror(BT->rchild) && judge_mirror(BT->lchild);
}
int main(){
cin>>n;
for(int i = 0; i < n; i++)
cin>>data[i];
BTree BT = NULL;
create(BT);
bool f1 = judge(BT), f2 = judge_mirror(BT);
if(f1){
cout<<"YES"<<endl;
print(BT);
}
else if(f2){
cout<<"YES"<<endl;
print_mirror(BT);
}
else cout<<"NO";
return 0;
}