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
结尾无空行
在建立树的时候,进行判断是否是二叉搜索树,但是最后一个测试点死活过去不,求助
#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;
typedef struct Node {
int val;
struct Node *left, *right;
}Node, *Tree;
vector<int> pre;
bool flag = true;
Tree create(int left, int right) {
if (left > right) {
return NULL;
}
Tree tree = new Node();
tree->val = pre[left];
tree->left = tree->right = NULL;
int i = left + 1;
int j;
if (pre[i] < pre[left]) {
while (pre[i] < pre[left] && i <= right) {
i++;
}
j = i;
while (pre[i] >= pre[left] && i <= right) {
i++;
}
if (i != right + 1) {
flag = false;
cout << "NO";
return 0;
}
} else {
while (pre[i] >= pre[left] && i <= right) {
i++;
}
j = i;
while (pre[i] < pre[left] && i <= right) {
i++;
}
if (i != right + 1) {
flag = false;
cout << "NO";
return 0;
}
}
if (!flag) {
return NULL;
}
tree->left = create(left + 1, j - 1);
tree->right = create(j, right);
return tree;
}
int t = 0;
void postorder(Tree tree) {
if (tree) {
postorder(tree->left);
postorder(tree->right);
if (!t) {
t = 1;
cout << tree->val;
} else {
cout << " " << tree->val;
}
}
}
int main() {
int n;
cin >> n;
pre.resize(n);
for (int i = 0; i < n; i++) {
cin >> pre[i];
}
Tree tree = create(0, n - 1);
if (!flag) {
} else {
cout << "YES" << endl;
postorder(tree);
}
return 0;
}
还是不用上面的难方法了,不好想,还是用简单清晰的吧,建立正确的二叉搜索树,判断前序与镜像前序是否相等,如果不等输出 NO,相等的话,如果是前序,输出后序,如果是镜像前序,输出镜像后序。
#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;
typedef struct Node {
int val;
struct Node *left, *right;
} Node, *Tree;
vector<int> pre;
vector<int> mirror_pre, post, mirror_post;
Tree create(Tree root, int x) {
if (root == NULL) {
root = new Node();
root->val = x;
root->left = root->right = NULL;
return root;
}
if (x < root->val) {
root->left = create(root->left, x);
} else {
root->right = create(root->right, x);
}
return root;
}
void preorder(Tree tree) {
if (tree) {
pre.push_back(tree->val);
preorder(tree->left);
preorder(tree->right);
}
}
void mirror_preorder(Tree tree) {
if (tree) {
mirror_pre.push_back(tree->val);
mirror_preorder(tree->right);
mirror_preorder(tree->left);
}
}
void postorder(Tree tree) {
if (tree) {
postorder(tree->left);
postorder(tree->right);
post.push_back(tree->val);
}
}
void mirror_postorder(Tree tree) {
if (tree) {
mirror_postorder(tree->right);
mirror_postorder(tree->left);
mirror_post.push_back(tree->val);
}
}
int main() {
int n;
cin >> n;
vector<int> v;
Tree tree = NULL;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
tree = create(tree, x);
}
preorder(tree);
mirror_preorder(tree);
postorder(tree);
mirror_postorder(tree);
if (pre == v || mirror_pre == v || post == v || mirror_post == v) {
printf("YES\n");
if (pre == v) {
for (int i = 0; i < post.size(); i++) {
if (!i) {
cout << post[i];
} else {
cout << " " << post[i];
}
}
} else if (mirror_pre == v) {
for (int i = 0; i < mirror_post.size(); i++) {
if (!i) {
cout << mirror_post[i];
} else {
cout << " " << mirror_post[i];
}
}
}
} else {
printf("NO");
}
return 0;
}