1043 Is It a Binary Search Tree (判断是否是二叉搜索树,且以后序遍历方式输出二叉搜索树)

给定一个整数序列,判断是否构成二叉搜索树的先序遍历,并在符合条件的情况下输出后序遍历序列。题目提供两种解题方法,包括暴力法和利用搜索二叉树的性质。

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

1043 Is It a Binary Search Tree (25 分)

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.如果是则以后序遍历方式输出二叉搜索树

                             不是就输出no

方法一:

 暴力法:               

//c++是-> 结构体用.
/**
二叉搜索树: 比根小的结点放在结点的左边 比根大或相等的结点放在结点右边

本题思路是:1.将输入的树 构建成一颗先序方式遍历的二叉搜索树(通过for循环,将每个结点放入树种) 将其放入一个新的向量数组中
            2.先序遍历构建的先序搜索树 与 题中输入的树进行对比 如果存在结点不同,则输出“NO ” 如果所有结点相同(且索引位置相同)
            则通过后序遍历输出 搜索树的结点

                  
**/

#include <cstdio>
#include<iostream>
#include<vector> 
using namespace std;
int n,i=0;
bool isMirror = false;
vector<int> prel; //存放题目中所给条件树 
vector<int> pre; //存放(二叉搜索)镜像树先序遍历后的结点 
struct Node{
	Node *left;
	Node *right;
	int value;
	Node(int val): value(val), left(nullptr), right(nullptr){} //构造方法,后面不用加; 
};
void findBFS(Node *&root, int value, bool Mirror){  //构建搜索二叉树 
	if(root == nullptr){
		root = new Node(value);
		return;
	}else if(!isMirror && value < root->value){
		findBFS(root->left, value, Mirror);
	}else if(!isMirror && value >= root->value){
		findBFS(root->right, value, Mirror); 
	}else if(isMirror && value < root->value){
		findBFS(root->right, value, Mirror);
	}else if(isMirror && value >= root->value){
		findBFS(root->left, value, Mirror);
	}
}

void preOrder(Node *&root, vector<int> &pre) { //先序遍历搜索二叉树 并将节点放入vertor pre中 
	if(root==nullptr) return;
	pre.push_back(root-> value);
	preOrder(root-> left, pre);
	preOrder(root-> right, pre);
}

void postOrder(Node *&root){  //后序遍历输出搜索二叉树的结点 
	if(root == nullptr) return;
	postOrder(root-> left);
	postOrder(root-> right);
	if(i++ != 0) printf(" ");
	printf("%d",root-> value); 
}

 
int main(){
	Node *root=nullptr; //没有初始化(难受)非全局变量一定要初始化 
	scanf("%d", &n);
	prel.resize(n);
	for(int i = 0;i < n; i++){
		scanf("%d", &prel[i]);
	}
	if(n>1 && prel[1] > prel[0]) isMirror = true;  //prel 不要写为pre  都不会报错的 
	for(int i = 0; i<prel.size(); i++){
		findBFS(root, prel[i], isMirror); // 构建搜索二叉树 
	}
	preOrder(root, pre);
	if(pre!=prel){
		printf("NO");
	}else{
		printf("YES\n");
		postOrder(root); 
	}
	return 0;
}

方法2:利用搜索二叉树的性质

/**
在1025通过 已知后序,中序遍历的二叉树 得到层序遍历二叉树   当然也可以得到后序遍历二叉树
           搜索树同样可以如此 得到了搜索树的先序遍历 ,根据搜索二叉树的性质,可以知道搜索树的
           中序遍历就是结点数值大小的 从小到大排列
           一般的树 需要知道先序,中序,  或者 后序,中序  因为只能确定 根结点的具体位置,至于第二个根结点的位置则需要
           知道左子树结点个数,右子树节点个数
           但是二叉搜索树根据二叉搜索树的性质 : 知道了先序遍历后 ,就可以确定左子树的结点都会小于 根,右子树的结点都会大于根,
           且先序遍历的二叉树的形势 都会是: a bcd efg (b, c, d<a) (e,f,g>a) 因此通过不断递归就可以构建出二叉树的后序遍历
           
**/

#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int> pre,post;
bool isMirror,flag=true;
void postOrder(int s, int e){ //
	if(!flag || s > e) return;
	int i=s+1, j=e;
	if(isMirror == false){
		while(i <= e && pre[s] > pre[i]) i++;
		while(j > s && pre[s] <= pre[j]) j--;	
	}else if(isMirror == true){
		while(i <= e && pre[s] <= pre[i]) i++;           //如果不是镜像,比结点大的树放在右边 
		while(j > s && pre[s] > pre[j])  j--;
	}
	if(i-j != 1){ //注意是i - j 因为最后遍历完成索引i在索引j的前面  以 a bcd efg为例子, 
				  // 此时i会指向e ,j会指向d(只要是二叉搜索树,就会满足此性质) 
		flag=false;
		return;              
	} 
	postOrder(s+1, i-1); 
	postOrder(i, e);
	post.push_back(pre[s]);
} 
int main(){
	scanf("%d", &n);		
	pre.resize(n);  //重新设置pre的容量范围,节约时间,直接分配空间大小,vector就不用动态重新构建容器的大小 
	for(int i=0; i<n; i++){
		scanf("%d", &pre[i]);
	}
	if(n > 1 && pre[1] - pre[0] > 0) isMirror=true;	
	postOrder(0, n-1);
	if(!flag)
		printf("NO");
	else{
		printf("YES\n");
		for(auto i = 0; i<post.size(); i++){
			if(i!=0) printf(" "); 
			printf("%d",post[i]);
		} 
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值