0
点赞
收藏
分享

微信扫一扫

PTA-二叉查找树(1043/1064/1099)

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

思路

根据给出的序列建BST,如果此序列为构建出的BST的先序序列,则输出此BST的后序序列;如果此序列为构建出的镜像BST的先序序列,则输出镜像BST的后序序列。
无需构建镜像BST,只需要在先序遍历BST时调换左右子树的遍历顺序即可实现镜像BST的遍历。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *left,*right;
};
vector<int> tree,pre,post,mirrorPre,mirrorPost;
int n;
void insert(node* &root,int data){
	if(root==NULL){
		root=new node;
		root->data=data;
		root->left=NULL; root->right=NULL;
		return ;
	}
	if(data<root->data) insert(root->left,data);
	else insert(root->right,data);
}
void preOrderTraverse(node* root){
	if(root==NULL) return ;
	pre.push_back(root->data);
	preOrderTraverse(root->left);
	preOrderTraverse(root->right);
}
void preOrderMirror(node* root){
	if(root==NULL) return ;
	mirrorPre.push_back(root->data);
	preOrderMirror(root->right);
	preOrderMirror(root->left);
}
void postOrderTraverse(node* root){
	if(root==NULL) return ;
	postOrderTraverse(root->left);
	postOrderTraverse(root->right);
	post.push_back(root->data);
}
void postOrderMirror(node* root){
	if(root==NULL) return ;
	postOrderMirror(root->right);
	postOrderMirror(root->left);
	mirrorPost.push_back(root->data);
}
int main()
{
	int data;
	node* root=NULL;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>data;
		tree.push_back(data);
		insert(root,data);		
	}
	preOrderTraverse(root);
	preOrderMirror(root);
	if(tree==pre){
		cout<<"YES"<<endl;
		postOrderTraverse(root);
		for(int i=0;i<post.size();i++){
			cout<<post[i];
			if(i!=post.size()-1) cout<<' ';
		}
	}
	else if(tree==mirrorPre){
		cout<<"YES"<<endl;
		postOrderMirror(root);
		for(int i=0;i<mirrorPost.size();i++){
			cout<<mirrorPost[i];
			if(i!=mirrorPost.size()-1) cout<<' ';
		}
	}
	else{
		cout<<"NO"<<endl;
	}
	return 0;
}

1064 Complete Binary Search Tree (30 分)

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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search 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:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

思路

题目要求利用给定权值,建立一棵完全二叉搜索树,我们可以利用性质解决问题:
1.中序遍历二叉搜索树得到的权值序列是从小到大的
2.完全二叉树可以用左孩子2*index,右孩子2*index+1,利用数组快速对所有结点完成遍历。
解法:我们对给定序列进行排序,这样得到的是二叉搜索树的中序序列,接着中序遍历给每一个节点带上权值(这里利用了完全二叉树的性质),然后顺序输出即为层序遍历序列。

#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> tree,level;
int num=0;
void inOrderTraverse(int index){
	if(index>n) return ;
	inOrderTraverse(index*2);
	level[index]=tree[num++];
	inOrderTraverse(index*2+1);
}
int main(){
	cin>>n;
	int data;
	level.resize(n+1);
	for(int i=0;i<n;i++){
		cin>>data;
		tree.push_back(data);
	}
	sort(tree.begin(),tree.end());
	inOrderTraverse(1);
	for(int i=1;i<n+1;i++){
		cout<<level[i];
		if(i!=n) cout<<' ';
	}
	return 0;
}

1099 Build A Binary Search Tree (30 分)

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.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

在这里插入图片描述

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then −1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42

Sample Output:

58 25 82 11 38 67 45 73 42

思路

对于每个结点都给出了左右孩子的序号,所以使用二叉树的静态写法最为方便。
还是利用了中序遍历二叉搜索树权值从小到大的性质,对给定序列进行排序之后中序赋值,最后bfs静态树输出序列即可。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	int left,right;
};
int n,num;
vector<node> tree(105);
vector<int> seq;
void inOrderTraverse(int index){
	if(tree[index].left>0) inOrderTraverse(tree[index].left);
	tree[index].data=seq[num++];
	if(tree[index].right>0) inOrderTraverse(tree[index].right);
}
void bfs(){
	int index;
	queue<int> que;
	que.push(1);
	while(!que.empty()){
		index=que.front();
		cout<<tree[index].data;
		//system("PAUSE");
		que.pop();
		if(tree[index].left>0) que.push(tree[index].left);
		if(tree[index].right>0) que.push(tree[index].right);
		if(!que.empty()) cout<<' ';
	}
}
int main()
{
	cin>>n;
	int left,right,data;
	for(int i=1;i<=n;i++){
		cin>>left>>right;
		tree[i].left=left+1;
		tree[i].right=right+1;
	}
	for(int i=0;i<n;i++){
		cin>>data;
		seq.push_back(data);
	}
	sort(seq.begin(),seq.end());
	inOrderTraverse(1);
	bfs();
	return 0;
}
举报

相关推荐

0 条评论