(PTA Advaced)1043.Is It a Binary Search Tree (二叉搜索树,树的遍历) C++

原题:https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856


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

题目大意

给定一个整数序列,判断其是否是一个二叉搜索树(或其镜像)的先序遍历,若是,输出其后序遍历结果。

方法1

假定它是一棵二叉搜索树,先根据前2个结点判断其是否是镜像。
然后对其递归恢复二叉树,然后判断所有结点是否均用上了,若不是,则不是一个二叉搜索树;若是,则后序遍历输出结果。

/* 1043 Is It a Binary Search Tree (25 分) */
#include<iostream>
#include<vector>

using namespace std;

int num[1010];
int len;
int flag = 1; // 1 表示BST,0表示镜像
int nodes = 0;  // 用到的结点数

typedef struct node {
	int data;
	struct node* left;
	struct node* right;
}node,*tree;

void Pre(int start, int end,tree& T) {
	// 先根遍历
	if (start > end) { // 超出了数组边界
		T = NULL;
		return;
	}
	T = new node;
	T->data = num[start]; // 根
	nodes++;
	int left_start = start + 1, left_end = start + 1;
	int right_start, right_end;

	if (flag == 1) {
		// 左边小
		while (left_end <= end && num[left_end] < num[start]) left_end++;
		right_end = right_start = left_end;
		while (right_end <= end && num[right_end] >= num[start]) right_end++;
	}
	else {
		// 左边大于等于
		while (left_end <= end && num[left_end] >= num[start]) left_end++;
		right_end = right_start = left_end;
		while (right_end <= end && num[right_end] < num[start]) right_end++;
	}
	// left_end 不包含在左子树中
	Pre(left_start, left_end - 1, T->left);
	Pre(right_start, right_end - 1, T->right);
}
void Post(node* T) {
	// 后序遍历
	if (!T) return;
	Post(T->left);
	Post(T->right);
	printf("%d ", T->data);
}
int main() {
	scanf("%d", &len);
	int i;
	for (i = 0; i < len; i++) scanf("%d", &num[i]);
	if (len >= 2 && num[1] >= num[0]) flag = 0; // 左根大于等于
	node* T;
	Pre(0, len-1, T);

	if (nodes < len) printf("NO\n");
	else {
		printf("YES\n");
		Post(T->left);
		Post(T->right);
		printf("%d\n", T->data);
	}
	system("pause");
	return 0;
}

方法2
在先序遍历恢复二叉树的时候,不用显式地建立二叉树,而是根据二叉搜索树的结点特性,先划分出左右子树,然后先遍历左子树,再遍历右子树,最后遍历根结点,在恢复的同时进行后序遍历,最后判断后序遍历序列的长度。

/* 1043 Is It a Binary Search Tree (25 分) */
#include<vector>
#include<iostream>
using namespace std;

int len; // 输入结点个数
int nodes; // 实际结点个数
int isMirror = 0; // 是否是镜像
vector<int> pre, post; // 先序和后序

void preOrder(int start, int end) {
	if (start > end) return;
	// 进行先序遍历
	int left_end = start + 1; // 左子树的终点,不包括
	int right_start = end;        // 右子树的起点,的前一位
	if (!isMirror) { // 左子树小
		while (left_end <= end && pre[left_end] < pre[start]) left_end++;
		while (right_start > start && pre[right_start] >= pre[start]) right_start--;
	}
	else { // 左子树大于等于
		while (left_end <= end && pre[left_end] >= pre[start]) left_end++;
		while (right_start > start && pre[right_start] < pre[start]) right_start--;
	}
	if (left_end - right_start != 1) return;  // 中间有隔离,不是二叉搜索树
	preOrder(start + 1, right_start);         // 恢复左子树
	preOrder(left_end, end);				 // 恢复右子树
	post.push_back(pre[start]);              // 建立根
}
int main() {
	scanf("%d", &len);
	pre.resize(len);
	int i;
	for (i = 0; i < len; i++) scanf("%d", &pre[i]);
	if (len >= 2 && pre[0] <= pre[1]) isMirror = 1;
	preOrder(0, len-1);
	if (post.size() < len) printf("NO\n");
	else {
		printf("YES\n%d",post[0]);
		for (int i = 1; i < post.size(); i++) printf(" %d", post[i]);
	}
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值