1 题目
1043 Is It a Binary Search Tree (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
2 解析
2.1 题意
判断给出序列是否为一颗二叉搜索树或者镜像二叉搜索树(交换左右孩子)的先序序列,如果是则输出“YES”以及该BST的后序序列或镜像BST的后序序列,否则输出“NO”
2.2 思路
- 1 新建BST
- 2 先序遍历BST,判断是否为BST为先序序列
- 如果是,则输出YES以及后序遍历序列
- 否则,先序遍历镜像BST(根右左),判断是否为镜像BST的先序遍历
- 如果是,输出YES以及镜像后序遍历序列(右左根);
- 否则,输出NO
3 参考代码
#include <cstdio>
#include <vector>
using std::vector;
int N;
const int MAXN = 1010;
int seq[MAXN];//输入序列
vector<int> preseq, mirpreseq, postseq, mirpostseq;
//先序序列、 镜像先序序列、后序序列、镜像后序序列
struct node//BST树
{
int data;
node* lchild;
node* rchild;
};
//新建结点
node* newNode(int v){
node* Node = new node;
Node->data = v;
Node->lchild = Node->rchild =NULL;
return Node;
}
//插入结点
void Insert(node* &root , int x){
if(root == NULL){
root = newNode(x);
return;
}
if(root->data > x){
Insert(root->lchild, x);
}else{
Insert(root->rchild, x);
}
}
//新建树
node* Create(int data[], int n){
node* root = NULL;
for (int i = 0; i < n; ++i)
{
Insert(root, data[i]);
}
return root;
}
//先序遍历
void preorder(node* root){
if(root == NULL) return;
preseq.push_back(root->data);
preorder(root->lchild);
preorder(root->rchild);
}
//镜像先序遍历
void mirpreorder(node* root){
if(root == NULL) return;
mirpreseq.push_back(root->data);
mirpreorder(root->rchild);
mirpreorder(root->lchild);
}
//后序遍历
void postorder(node* root){
if(root == NULL) return;
postorder(root->lchild);
postorder(root->rchild);
postseq.push_back(root->data);
}
//镜像后序遍历
void mirpostorder(node* root){
if(root == NULL) return;
mirpostorder(root->rchild);
mirpostorder(root->lchild);
mirpostseq.push_back(root->data);
}
int main(int argc, char const *argv[])
{
scanf("%d", &N);
for (int i = 0; i < N; ++i)
{
scanf("%d", &seq[i]);
}
node* root = Create(seq, N);//创建树
preorder(root);//先序遍历树
bool flag = false;//判断输入序列是否为BST树的先序序列或者镜像BST树的先序序列
for (int i = 0; i < N; ++i)//输入序列是否为BST树的先序序列
{
if(seq[i] != preseq[i]){
break;
}
if(i == N - 1){
flag = true;
}
}
if(flag == true){
printf("YES\n");
postorder(root);//后序遍历
for (int i = 0; i < N; ++i)//输出后序序列
{
printf("%d", postseq[i]);
if(i < N - 1) printf(" ");
}
}else{
mirpreorder(root);
for (int i = 0; i < N; ++i)//判断输入序列是否为镜像BST树的先序序列
{
if(seq[i] != mirpreseq[i]){
break;
}
if(i == N - 1){
flag = true;
}
}
if(flag == true){//如果是镜像BST树的先序序列
printf("YES\n");
mirpostorder(root);//镜像后序序列
for (int j = 0; j < N; ++j)
{
printf("%d", mirpostseq[j]);
if(j < N - 1) printf(" ");
}
}else{
printf("NO\n");
}
}
return 0;
}

本文介绍了一种算法,用于判断给定的整数序列是否为二叉搜索树或其镜像的先序遍历序列。通过构建二叉搜索树并比较先序遍历结果,算法能够准确判断并输出相应的后序遍历序列。

被折叠的 条评论
为什么被折叠?



