PAT甲級1004,1020解題報告

 

1004 Counting Leaves (30 point(s))

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

題目大意:給一棵樹,求出各個層次中葉子節點的個數。

解題思路:趁這兩天覆習數據結構,樹圖的題目這兩天寫寫完,其實樹圖就是套個模板。。尤其是樹,幾乎可以說凡樹必遞歸。這題就DFS搜索一下這棵樹就行了.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
using namespace std;
map<int, vector<int>> tree;
map<int, int> res;
void Dfs(int node,int rank){
	//is leave
	if (tree[node].empty()) {
		res[rank]++;
		return;
	}
	for (auto i = tree[node].begin(); i != tree[node].end(); i++) {
		Dfs(*i, rank + 1);
	}
}
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int pid, k;
		scanf("%d %d", &pid, &k);
		for (int j = 0; j < k; j++) {
			int id;
			scanf("%d", &id);
			tree[pid].push_back(id);
		}
	}
	Dfs(1, 0);
	int sum = 0;
	int num;
	for (int i = 0;; i++) {
		sum += res[i];
		if (sum == n - m) {
			num = i;
			break;
		}
	}
	for (int i = 0; i<=num; i++) {
		if (i != num)
			cout << res[i] << " ";
		else
			cout << res[i] << endl;
	}

	return 0;
}

1020 Tree Traversals (25 point(s))

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

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

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

題目大意:給個後序遍歷和中序遍歷,求出層次遍歷。

解題思路:和做數據結構題時候差不多思路吧,先找根,然後左右分開,再繼續找根···一個遞歸的過程就出來了,然後層次遍歷就是個模板BFS一下。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<queue>
using namespace std;
vector<int> post;
vector<int> in;
vector<int> lev;
struct node {
	int data;
	node* left;
	node* right;
};
node* buildTree(int postLeft, int postRight, int inLeft, int inRight) {
	if (postLeft > postRight) return NULL;
	int pos;
	for (int i = inLeft; i <= inRight; i++) {
		if (in[i] == post[postRight]) {
			pos = i;
			break;
		}
	}
	node* cur=new node;
	cur->data = post[postRight];
	cur->left = buildTree(postLeft, postLeft + pos - inLeft - 1, inLeft, pos - 1);
	cur->right = buildTree(postLeft + pos - inLeft, postRight - 1, pos + 1, inRight);
	return cur;
}
void Bfs(node* tree) {
	queue<node*> q;
	q.push(tree);
	while (!q.empty()) {
		node* cur = q.front();
		lev.push_back(cur->data);
		q.pop();
		if (cur->left !=NULL) q.push(cur->left);
		if (cur->right != NULL) q.push(cur->right);
	}
	return;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int cur;
		scanf("%d", &cur);
		post.push_back(cur);
	}
	for (int i = 0; i < n; i++) {
		int cur;
		scanf("%d", &cur);
		in.push_back(cur);
	}
	node* tree = buildTree(0, n - 1, 0, n - 1);
	Bfs(tree);
	for (int i = 0; i < n; i++) {
		if (i != n - 1) {
			printf("%d ", lev[i]);
		}
		else {
			printf("%d\n", lev[i]);
		}
	}
	return 0;
}

 

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