原题干:
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
生词:
recursively:递归
大意:给出一棵二叉排序树的序列,判断给定的这个序列是不是二叉排序树的先序序列,或者是二叉树的镜像先序序列(即将二叉排序树每个节点左右孩子都调换),如果是,输出YES,并在下一行给出该二叉排序树的后序/镜像后序序列。如果不是,输出NO。
代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node{
int data;
node *lchild, *rchild;
};
//二叉查找树的插入方法
void insert(node* &root, int data){
if(root == NULL){
root = new node;
root->data= data;
root->lchild= root->rchild= NULL;
return;
}
if(root->data <= data)
insert(root->lchild, data);
else insert(root->rchild, data);
}
//递归先序遍历
void preorder(node *root, vector<int> &pre){
if(root == NULL) return;
pre.push_back(root->data);
preorder(root->lchild, pre);
preorder(root->rchild, pre);
}
//递归镜像先序遍历
void preorder_mir(node *root, vector<int> &pre_mir){
if(root == NULL) return;
pre_mir.push_back(root->data);
preorder_mir(root->rchild, pre_mir);
preorder_mir(root->lchild, pre_mir);
}
//递归后序遍历
void postorder(node *root, vector<int> &post){
if(root == NULL) return;
postorder(root->lchild, post);
postorder(root->rchild, post);
post.push_back(root->data);
}
//递归镜像后序遍历
void postorder_mir(node *root ,vector<int> &post_mir){
if(root == NULL) return;
postorder_mir(root->rchild, post_mir);
postorder_mir(root->lchild, post_mir);
post_mir.push_back(root->data);
}
//输出容器
void Print(vector<int> &V){
for(int i = 0; i < V.size(); i++){
if(i) cout << ' ';
cout << V[i];
}
}
int main(){
node *T = NULL;
int cnt;
cin >> cnt;
//定义容器
vector<int> input, pre, post, pre_mir;
//读取数据
for(int i = 0; i < cnt; i++){
int num;
cin >> num;
input.push_back(num);
insert(T, num);
}
//执行先序遍历
preorder(T, pre);
//执行镜像先序遍历
preorder_mir(T, pre_mir);
if(pre == input){
postorder(T, post);
cout << "YES" << endl;
Print(post);
}else if(pre_mir == input){
postorder_mir(T, post);
cout << "YES" << endl;
Print(post);
}else cout << "NO" << endl;
return 0;
}
如果觉得把镜像与非镜像分开写很臃肿,还可以这么写:
#include <iostream>
#include <vector>
using namespace std;
struct node{
int data;
node *lchild, *rchild;
};
/**
* @全局变量定义区
* tree为全局所用的树
* input为输入序列
* pre为先序序列,m_pre为镜像先序序列
* pos为后序序列,m_pos为镜像后序序列
*/
node *tree = NULL;
vector<int> input, pre, m_pre, pos, m_pos;
/**
* 树的插入数据方法
* 参数:树根、欲插入的数据
*/
void insert(node* &root, int data){
if(!root){ //插入点
root = new node;
root->data = data;
root->lchild = root->rchild = NULL;
return;
}
if(root->data <= data) //小于等于,插入左子树
insert(root->lchild, data);
else insert(root->rchild, data); //大于,插入右子树
}
/**
* 树的先序遍历方法
* mirror为真的时候,执行镜像遍历
* 非镜像遍历先序序列存入pre容器
* 镜像遍历先序序列存入m_pre容器
*/
void preorder(node* root, bool mirror){
if(!root) return;
if(!mirror){ //非镜像遍历
pre.push_back(root->data);
preorder(root->lchild, mirror);
preorder(root->rchild, mirror);
}else{ //镜像遍历
m_pre.push_back(root->data);
preorder(root->rchild, mirror);
preorder(root->lchild, mirror);
}
}
/**
* 树的后序遍历方法
* mirror为真的时候,执行镜像遍历
* 非镜像遍历后序序列存入pos容器
* 镜像遍历后序序列存入m_pos容器
*/
void postorder(node* root, bool mirror){
if(!root) return;
if(!mirror){
postorder(root->lchild, mirror);
postorder(root->rchild, mirror);
pos.push_back(root->data);
}else{
postorder(root->rchild, mirror);
postorder(root->lchild, mirror);
m_pos.push_back(root->data);
}
}
/**
* 容器的遍历输出方法
* 输出后序遍历序列
* mirror为真的时候,使用镜像容器
*/
void print_vector(bool mirror){
if(mirror){ //镜像,使用m_pos容器
for(int i = 0; i < m_pos.size(); i++){
cout << m_pos[i];
if(i != m_pos.size()-1) cout << " ";
}
}else{ //非镜像,使用pos容器
for(int i = 0; i < pos.size(); i++){
cout << pos[i];
if(i != pos.size()-1) cout << " ";
}
}
}
int main(){
int cnt, tmp;
cin >> cnt;
for(int i = 0; i < cnt; i++){
cin >> tmp;
insert(tree, tmp);
input.push_back(tmp);
}
preorder(tree, false); //执行非镜像遍历
if(input == pre){ //是非镜像遍历的先序序列
cout << "YES" << endl;
postorder(tree, false);
print_vector(false);
}else{
preorder(tree, true); //执行镜像遍历
if(input == m_pre){ //是镜像遍历的先序序列
cout << "YES" << endl;
postorder(tree, true);
print_vector(true);
}else cout << "NO" << endl;//既不是镜像,也不是非镜像
}
return 0;
}