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;
}