PAT甲 7-4 Cartesian Tree (30分) 兩種解法

7-4 Cartesian Tree (30分)

A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.

CTree.jpg

Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.

Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.

Output Specification:

For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

10
8 15 3 4 1 5 12 10 18 6

Sample Output:

1 3 5 8 4 6 15 10 12 18

思路:

利用最小堆的性質,根爲當前子樹最小的值,得到根的下標,而中序遍歷爲LNR,得到根下標即可分開左子樹和右子樹。
方法一:建樹+BFS即可,詳見代碼。
方法二:不必建樹,在遍歷得到根節點的同時記錄樹深度,並將各層節點存入深度數字level[ ], 之後從遍歷0~最深層依次輸出即可。

在這裏插入圖片描述

Code(方法一):

#include<cstdio> 
#include<iostream> 
#include<vector> 
#include<map>
#include<string>
#include<queue>
#include<cctype>
#include<algorithm>
using namespace std;
const int INF = 0x3fffffff;
const int maxn = 210;
struct node{
	int data;
	node* lchild,*rchild;
	node(int x){
		data = x; lchild = rchild = NULL;
	}
};
vector<int> in;
int find_min(int L,int R){
	int Min = INF,index = -1;
	for(int i = L; i <= R; i++){
		if(in[i] < Min){
			Min = in[i];
			index = i;
		}
	}
	return index;
}
node* build(int L,int R){
	if(L > R) return NULL;
	int X = find_min(L,R);
	node* root = new node(in[X]);
	root->lchild = build(L,X-1);
	root->rchild = build(X+1,R);
	return root;
}
int cnt = 0,n;
void BFS(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* now = q.front();
		q.pop();
		printf("%d",now->data);
		cnt++;
		if(cnt != n) printf(" ");
		else printf("\n");
		if(now->lchild)
			q.push(now->lchild);
		if(now->rchild)
			q.push(now->rchild);
	}
}
int main(){
	scanf("%d",&n);
	in.resize(n);
	for(int i = 0; i < n; i++)
		 scanf("%d",&in[i]);
	node* root = build(0,n-1);
	BFS(root);
	return 0;
} 

Code(方法二):

#include<cstdio> 
#include<iostream> 
#include<vector> 
#include<algorithm>
using namespace std;
const int INF = 0x3fffffff;
const int maxn = 210;
struct node{
	int data;
	node* lchild,*rchild;
	node(int x){
		data = x; lchild = rchild = NULL;
	}
};
vector<int> in;
int find_min(int L,int R){
	int Min = INF,index = -1;
	for(int i = L; i <= R; i++){
		if(in[i] < Min){
			Min = in[i];
			index = i; 
		}
	}
	return index;
}
int MaxH = -1;
vector<int> level[maxn];
void build(int L,int R,int height){
	if(L > R) return ;
	if(height > MaxH) MaxH = height;
	int X = find_min(L,R);
	level[height].push_back(in[X]);
	build(L,X-1,height+1);
	build(X+1,R,height+1);
}
int cnt = 0,n;
int main(){
	scanf("%d",&n);
	in.resize(n);
	for(int i = 0; i < n; i++)
		 scanf("%d",&in[i]);
	build(0,n-1,0);
	for(int i = 0; i <= MaxH; i++){
		if(i == 0){
			printf("%d",level[i][0]);
		}
		else {
			for(int j = 0; j < level[i].size();j++)
				printf(" %d",level[i][j]);
		}
	}
	printf("\n");
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章