前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT List Leaves

PAT List Leaves

作者头像
刘开心_1266679
发布2019-02-14 15:27:14
3950
发布2019-02-14 15:27:14
举报

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.

Sample Input:

代码语言:javascript
复制
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

代码语言:javascript
复制
4 1 5

题目大意:

       要求按照从上到下、从左到右的顺序输出所给树的叶子结点。输入第一行为结点个数,以下各行为每个结点的左孩子和右孩子的结点编号。例如,样例所表示的树为:

解题思路:

       1、建树。输入中带有字符‘-’,为了避免空白字符的干扰,采用cin输入最方便。建立结构体树的数组,该数组的编号就代表它是第几个结点,上图的结点编号就是树结构体数组,孩子编号就是树结构体数组里的内容,只需要找到树根就可以根据上图的表构建出树,树根的找法是孩子编号里没有的那个数就是根,上图孩子编号没有3,说明3是根结点。

2、层次遍历二叉树。其实就是宽度优先搜索,宽搜用队列实现,所以用一个数组q[]模拟队列即可,遍历的过程中把叶子结点打印出来。

代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

struct Tree
{
	char rchild, lchild;
	void clear()
	{
		rchild = 0;
		lchild = 0;
	}
};

Tree head[15];
int q[15];  //模拟队列 

int findRoot(Tree head[], int n)
{
	int isRoot[15];  //相当于布尔数组 
	char left, right;
	memset(isRoot, 0, sizeof(isRoot));
	for(int i = 0; i < n; i++)
	{
		left = head[i].lchild - '0';
		if(left >= 0)
			isRoot[left] = 1;
		right = head[i].rchild - '0';
		if(right >= 0)
			isRoot[right] = 1;
	}
	for(int i = 0; i < n; i++)
	{
		if(isRoot[i] == 0)
			return i;
	}
}

void bfs(Tree head[], int n, int root)
{
	int first = 1;//判断是不是第一个输出 
	int rear = 0, front = 0;//队列首尾 
	q[rear++] = root;
	while(front != rear)//当队列不为空 
	{
		if(head[q[front]].lchild != '-')
		{
			q[rear] = head[q[front]].lchild - '0';
			rear++;
		}
		if(head[q[front]].rchild != '-')
		{
			q[rear] = head[q[front]].rchild - '0';
			rear++;
		}
		if(head[q[front]].lchild == '-' && head[q[front]].rchild == '-')
		{
			if(first) 
			{
				cout << q[front];
				first = 0;
			}
			else
				cout << " " << q[front];
		} 
		front++;
	}
}

int main()
{
//	freopen("listLeaves.txt", "r", stdin);
	int n;
	int root;
	char c, d;
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 0; i < n; i++)
			head[i].clear();
		memset(q, 0, sizeof(q));
		for(int i = 0; i < n; i++)
		{
			cin >> head[i].lchild >> head[i].rchild;
		}
		root = findRoot(head, n);
		bfs(head, n, root);
	}
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年04月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档