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
题意
给出一个序列,问它是否是二叉树或者镜像二叉树的先序遍历。如果是则输出该树的后序遍历。
思路
读取输入并建立二叉树。使用dfs获取二叉树先序序列和后序序列。以及一个修改的dfs(镜像二叉树等于将原二叉树的左右子树交换)获取镜像二叉树的二叉树先序序列和后序序列。最后比较输出结果即可。
代码
#include <cstdio>
#define MAX_N 1000
struct Node{
int value;
Node* left;
Node* right;
Node() :left(NULL), right(NULL){}
};
Node* root = NULL;
// 插入结点
void insert(Node*&x, int value){
if(x == NULL){
x = new Node;
x->value = value;
}else if(value < x->value){
insert(x->left, value);
}else{
insert(x->right, value);
}
}
// 遍历
int treePreOrder[MAX_N], p = 0, mirrorPreOrder[MAX_N], q = 0;
int treePostOrder[MAX_N], m = 0, mirrorPostOrder[MAX_N], n = 0;
void treeDfs(Node*&x){// 同时获得先序和后序序列
if(x == NULL) return;
treePreOrder[p++] = x->value;
treeDfs(x->left);
treeDfs(x->right);
treePostOrder[m++] = x->value;
}
void mirrorDfs(Node*&x){// 同时获得先序和后序序列
if(x == NULL) return;
mirrorPreOrder[q++] = x->value;
mirrorDfs(x->right);
mirrorDfs(x->left);
mirrorPostOrder[n++] = x->value;
}
// 比较两个序列是否相同
bool compare(int a[], int b[], int N){
for(int i = 0; i < N; i++){
if(a[i] != b[i]) return false;
}
return true;
}
// 打印序列
void printOrder(int order[], int N){
printf("YES\n%d", order[0]);
for(int i = 1; i < N;i++){
printf(" %d", order[i]);
}
}
int preOrder[MAX_N];
int main() {
// 读取输入并低估建树
int N;
scanf("%d", &N);
for(int i = 0, t; i < N; i++){
scanf("%d", &t);
insert(root, t);
preOrder[i] = t;
}
// 遍历
treeDfs(root);
mirrorDfs(root);
// 对比序列并输出结果
if(compare(preOrder, treePreOrder, N)){
printOrder(treePostOrder, N);
}else if(compare(preOrder, mirrorPreOrder, N)){
printOrder(mirrorPostOrder, N);
}else{
printf("NO");
}
return 0;
}
这是一个关于判断给定序列是否为二叉搜索树或其镜像的先序遍历的问题。需要输出如果符合条件的后序遍历序列。通过构建二叉树并进行深度优先搜索(DFS)来解决。

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



