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, 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.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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
这道题对自己来说还是非常艰难,最后竟然写了将近100行,也写了很久。
- 根据元素的性质(大于等于还是小于根节点)来判断是1.BST 2.Mirror BST,同时划分出左子树。
- 在右子树中进行遍历,查看是否是BST。
- 递归check左子树和右子树。
#include<cstdio> const int maxn = 1010; int array[maxn] = { 0 }; int flag; //if flag = 1, BST ; flag = 0, mirror bool result = true; int cnt = 0; struct node { int left, right; }tree[maxn]; int check(int begin, int end) { int root = begin; if (begin == end) return begin; if (begin > end) return -1; if (flag == 1) { int i, j; //[begin + 1, i - 1]是左子树,[i,end]是右子树 for (i = begin + 1; i <= end && array[i] < array[begin]; i++); for (j = i; j <= end; j++) { if (array[j] < array[begin]) { result = false; return -1; } } tree[root].left = check(begin + 1, i - 1); tree[root].right = check(i, end); } else if (flag == 0) { int i, j; //[begin+1, i - 1]是左子树,[i,end]是右子树 for (i = begin + 1; i <= end && array[i] >= array[begin]; i++); for (j = i; j <= end; j++) { if (array[j] >= array[begin]) { result = false; return -1; } } tree[root].left = check(begin + 1, i - 1); tree[root].right = check(i, end); } return root; } void output(int root){ if(tree[root].left != -1) output(tree[root].left); if (tree[root].right != -1) output(tree[root].right); if (cnt == 0) { printf("%d", array[root]); } else { printf(" %d", array[root]); } cnt++; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &array[i]); tree[i].left = -1; tree[i].right = -1; } if (array[1] < array[0]) flag = 1; else flag = 0; int root = check(0, N - 1); if (result) { printf("YES\n"); output(root); } else printf("NO\n"); return 0; }
看了《算法笔记》的标准答案,有种醍醐灌顶的感觉。
其实按照标准来解就好了,直接把先序序列插入BST,然后按两种方式输出先序序列进行比较,就可以判断出树是BST 还是Mirror BST,如果与两个序列都不相等,就直接输出No,最后在输出后序序列。
思路清晰,以不变应万变。
12.1:
再看这道题,依然要依靠标准答案。
教训:
不要被题意签着鼻子走,既然是BST,那么直接建一个BST就好了啊(一定是unique的)
想清楚实现的逻辑,代码再长,也是很简单的
#include<cstdio>
#include<vector>
using namespace std;
vector<int> origin, pre, pre_mirror, post;
//vector<int> post;
const int maxn = 1050;
struct node {
int data;
node* left;
node* right;
};
void insert(node*& root, int data) {
if (root == NULL) {
root = new node;
root->data = data;
root->left = root->right = NULL;
return;
}
if (data < root->data) insert(root->left, data);
else insert(root->right, data);
}
void PreOrder(node* root) {
if (root == NULL)
return;
pre.push_back(root->data);
PreOrder(root->left);
PreOrder(root->right);
}
void PreOrderMirror(node* root) {
if (root == NULL)
return;
pre_mirror.push_back(root->data);
PreOrderMirror(root->right);
PreOrderMirror(root->left);
}
void PostOrder(node* root) {
if (root == NULL)
return;
PostOrder(root->left);
PostOrder(root->right);
post.push_back(root->data);
}
void PostOrderMirror(node* root) {
if (root == NULL)
return;
PostOrderMirror(root->right);
PostOrderMirror(root->left);
post.push_back(root->data);
}
int main() {
int n, data;
scanf("%d", &n);
node* root = NULL; //root是一个空指针,因为还没有插入数据
for (int i = 0; i < n; i++) {
scanf("%d", &data);
insert(root, data);
origin.push_back(data);
}
PreOrder(root);
PreOrderMirror(root);
if (origin == pre) {
printf("YES\n");
PostOrder(root);
for (auto it = post.begin(); it != post.end(); it++) {
if (it != post.begin())
printf(" %d", *it);
else
printf("%d", *it);
}
}
else if (origin == pre_mirror) {
printf("YES\n");
PostOrderMirror(root);
for (auto it = post.begin(); it != post.end(); it++) {
if (it != post.begin())
printf(" %d", *it);
else
printf("%d", *it);
}
}
else {
printf("NO\n");
}
}