1043 Is It a Binary Search Tree (25 分)(24分代码,求助)

该博客主要讨论如何判断给定的整数序列是否为二叉搜索树的前序遍历或其镜像,并在确定后输出相应的后序遍历序列。通过递归建立树结构并比较前序遍历,实现了对序列有效性的检查,并在符合条件的情况下,输出后序遍历结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值