算法 树2 List Leaves

全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案汇总

题目:Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(≤10),它是树中节点的总数,因此节点的编号从0到N−1。接下来是N行,每行对应一个节点,并给出节点的左右子节点的索引。如果孩子不存在,将在该位置加“-”。任何一对孩子都被一个空格隔开。

输出规格:

对于每个测试用例,按自上而下和从左到右的顺序在一行中打印所有叶子的索引。任何相邻的数字之间必须正好有一个空格,并且在行尾没有多余的空格。

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5

解答:

乍看一下这个题,感觉,和上面那个题一样?虽然不完全一样但是相当类似,不过这个题更简单,每行输入连元素都省略了。

现在先附上前面几行代码:

#include <iostream>
#include <queue>
using namespace std;

#define MaxTree 100
#define ElementType char
#define Tree int
#define Null -1

queue<int> myquene;

struct TreeNode
{
	Tree Left;
	Tree Right;
} T1[MaxTree], T2[MaxTree];
int check[MaxTree];
int leafsNum[MaxTree];
int leafsCount = 0;

在这里我们还是使用上一个题目构建好的代码,只不过TreeNode里面少了一个element成员。

因为这个题是层序遍历的,所以需要用到队列,我们直接使用stl里面的队列即可。

这个check数组和上题一样,是用来找根节点的。树的构建方法也是和上个题几乎完全一样,只是不用再输入一个element元素了。

重复的代码我们不再贴上(因为最后会给出整体的直接测试可通过的代码)。

之所以需要一个leafsNum数组来存叶节点,是因为我们最后打印的时候,最后面不能加一个空格,也就是说,如果我们输出的每个数据是 数字+空格,最后会多一个空格,这样就错了(我没验证,但是既然题目说行尾没有多余的空格我们就严格照办)

现在我们需要写一个函数来进行层序遍历:

void printTree(Tree R1) {
	myquene.push(R1);
	while (!myquene.empty()) {
		int Tadd = myquene.front();
		myquene.pop();
		if (T1[Tadd].Left == Null && T1[Tadd].Right == Null)
			leafsNum[leafsCount++] = Tadd;
		if (T1[Tadd].Left != Null)
			myquene.push(T1[Tadd].Left);
		if (T1[Tadd].Right != Null)
			myquene.push(T1[Tadd].Right);
	}
	for (int i = 0;i < leafsCount - 1;i++) {
		cout << leafsNum[i] << " ";
	}
	cout << leafsNum[leafsCount - 1];
}

这种算法的思想在数据结构视频里讲得很详细,就是通过队列进行的层序遍历,这里就不再赘述了。我们只打印左右两边都是空的叶节点。

最后全部代码附上:


#include <iostream>
#include <queue>
using namespace std;

#define MaxTree 100
#define ElementType char
#define Tree int
#define Null -1

queue<int> myquene;

struct TreeNode
{
	Tree Left;
	Tree Right;
} T1[MaxTree], T2[MaxTree];
int check[MaxTree];
int leafsNum[MaxTree];
int leafsCount = 0;
Tree BuildTree(struct TreeNode T[]);
void printTree(Tree R1);

int main()
{
	Tree R1;
	R1 = BuildTree(T1);
	
	if (R1 == -1) {
		printf("Yes\n");
	}
	else{
		printTree(R1);
	}

	system("pause");
	return 0;
}


Tree BuildTree(TreeNode T[])
{
	int N;
	int Root = -1;
	cin >> N;
	if (N) {
		for (int i = 0; i<N; i++) check[i] = 0;//先都初始化为0
		for (int i = 0; i<N; i++) {
			char cl, cr;//之前不小心把这个写成了int,结果出现奇怪的错误

			cin >> cl >> cr;
			if (cl != '-') {
				T[i].Left = cl - '0';
				check[T[i].Left] = 1;
			}
			else T[i].Left = Null;
			//对cr的对应处理
			if (cr != '-') {
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else T[i].Right = Null;
		}
		int i;
		for (i = 0; i<N; i++)
			if (!check[i]) break;
		Root = i;
	}
	return Root;
}

void printTree(Tree R1) {
	myquene.push(R1);
	while (!myquene.empty()) {
		int Tadd = myquene.front();
		myquene.pop();
		if (T1[Tadd].Left == Null && T1[Tadd].Right == Null)
			leafsNum[leafsCount++] = Tadd;
		if (T1[Tadd].Left != Null)
			myquene.push(T1[Tadd].Left);
		if (T1[Tadd].Right != Null)
			myquene.push(T1[Tadd].Right);
	}
	for (int i = 0;i < leafsCount - 1;i++) {
		cout << leafsNum[i] << " ";
	}
	cout << leafsNum[leafsCount - 1];
}

测试全部通过。

发布了174 篇原创文章 · 获赞 394 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tiao_god/article/details/105049801