PAT--1064 Complete Binary Search Tree

題目鏈接:https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568

題目大意:給出n個數,讓你建立一棵完全二叉樹,並輸出層序遍歷的結果。

分析:先把給出的數組排序,再求出要建立的樹有多少層。因爲中序遍歷的結果就是順序的,所以按照中序遞歸,每遞歸一層,層數減一,求出當前根節點是第幾個數,然後先往左子樹走,再給當前根節點賦值,最後往右子樹走。當只剩下一個節點或者層數爲1時,賦值,返回。最後層序遍歷一下就可以了。

數組開大點。。。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, pos, a[N], ans[N], sum[N];
void init() {
	sum[1] = 1;
	for(int i = 2; i < 15; i++)
		sum[i] = sum[i - 1] + pow(2, i - 1);
}
void dfs(int x, int cnt, int l, int r) {
	if(l > r) return ;
	if(l == r || cnt == 1) {
		ans[x] = a[pos++];
		return ;
	}
	int num = r - l + 1;
	int rt = (sum[cnt - 1] - 1) / 2;
	int cha = sum[cnt] - sum[cnt - 1];
	rt += min(num - sum[cnt - 1], cha / 2);
	rt++;
	rt = rt + l - 1;
	dfs(x<<1, cnt - 1, l, rt - 1);
	ans[x] = a[rt];
	pos++;
	dfs(x<<1|1, cnt - 1, rt + 1, r);
}
void solve() {
	queue<int> q;
	q.push(1);
	int d = 0;
	while(!q.empty()) {
		int tmp = q.front();
		q.pop();
		if(d > 0) printf(" ");
		printf("%d", ans[tmp]);
		d++;
		if(ans[tmp<<1] != -1) q.push(tmp<<1);
		if(ans[tmp<<1|1] != -1) q.push(tmp<<1|1);
	}
	printf("\n");
}
int main() {
	init();
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	int cnt = 1;
	while(n >= sum[cnt]) cnt++;
	if(n - sum[cnt] == 0) cnt--;
	pos = 1;
	memset(ans, -1, sizeof ans);
	dfs(1, cnt, 1, n);
	solve();
	return 0;
}

鏈表實現

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, pos, a[N], ans[N], sum[N];
struct node {
	int data;
	node *left, *right;
};
struct node *root = NULL;
void init() {
	sum[1] = 1;
	for(int i = 2; i < 15; i++)
		sum[i] = sum[i - 1] + pow(2, i - 1);
}
void dfs(node * &root, int cnt, int l, int r) {
	if(l > r) return ;
	if(l == r || cnt == 1) {
		root = new node();
		root->data = a[pos++];
		root->left = root->right = NULL;
		return ;
	}
	if(root == NULL) {
		root = new node();
		root->left = root->right = NULL;
	}
	int num = r - l + 1;
	int rt = (sum[cnt - 1] - 1) / 2;
	int cha = sum[cnt] - sum[cnt - 1];
	rt += min(num - sum[cnt - 1], cha / 2);
	rt++;
	rt = rt + l - 1;
	dfs(root->left, cnt - 1, l, rt - 1);
	root->data = a[rt];
	pos++;
	dfs(root->right, cnt - 1, rt + 1, r);
}
void solve() {
	queue<node*> q;
	q.push(root);
	int d = 0;
	while(!q.empty()) {
		node *tmp = q.front();
		q.pop();
		if(d > 0) printf(" ");
		printf("%d", tmp->data);
		d++;
		if(tmp->left != NULL) q.push(tmp->left);
		if(tmp->right != NULL) q.push(tmp->right);
	}
	printf("\n");
}
int main() {
	init();
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	int cnt = 1;
	while(n >= sum[cnt]) cnt++;
	if(n - sum[cnt] == 0) cnt--;
	pos = 1;
	dfs(root, cnt, 1, n);
	solve();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章