PTA || 03-树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 NNN (≤10\le 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1N-1N1. Then NNN 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:

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

Sample Output:

4 1 5
/*
	Name: 树2 List Leaves.cpp
	Copyright: 
	Author: xuuyann
	Date: 20/10/18 09:57
	Description: 
*/
#include <stdio.h>
#include <stdlib.h>
#define MaxTree 10
#define Tree int
#define ElementType int
#define Null -1

//结点结构定义 
struct TreeNode {
	Tree Left;
	Tree Right;
}T[MaxTree];

//队列的顺序存储结构定义
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
	ElementType *Data;
	Position Front;
	Position Rear;
	int MaxSize;
}; 
typedef PtrToQNode Queue;

Tree BuildTree(struct TreeNode T[], int N);  //建树
void LeaveSearch(Tree R, int N);//查找叶节点并返回
Queue CreateQueue(int MaxSize);//建立循环队列
int IsEmpty(Queue Q);//判断队列是否为空
void AddQ(Queue Q, ElementType X);//入队列 
ElementType DeleteQ(Queue Q);//出队列并返回出队列的元素 

//程序框架搭建
int main()
{
	Tree R;//树(子树)的根节点
	int N;
	scanf("%d\n",&N);
	R = BuildTree(T, N); //建树
	LeaveSearch(R, N);//查找叶节点并返回 
	 
	return 0;
 } 
 
Tree BuildTree(struct TreeNode T[], int N)
{
	int i; //树的层数
	Tree Root;
	char cl, cr;
	int check[10] = {0,};
	if (!N) Root = Null;
	if (N){ //树不空 
		for (i=0; i<N; i++) check[i] = 0;
		for (i=0; i<N; i++){
			scanf("%c %c\n",&cl, &cr);
			if (cl != '-'){
				T[i].Left = cl-'0';
				check[T[i].Left ] = 1;
			}else T[i].Left = Null;
			if (cr != '-'){
				T[i].Right = cr-'0';
				check[T[i].Right ] = 1;
			}else T[i].Right = Null;
		}
		for (i=0; i<N; i++) if (!check[i]) break;
		Root = i;
	}
	return Root; //返回该树的根节点 
}

//需要建立一个队列,实现层序遍历 
void LeaveSearch(Tree R, int N)//有问题 
/*	题目要求按每一层顺序输出,则可使用层序遍历,
	在遍历算法中增加叶节点的检测条件即可	 */ 
{
	/*******************************
	存在以下几种情况:
	1.树空;
	2.只有一个结点
	3.非空且有多个结点 
	********************************/
	Queue Q;
	Tree R_1;
	int flag = 1;
	int cnt = 0;
	Q = CreateQueue(MaxTree);
	if (R == Null) printf("0");
	if (T[R].Left == Null && T[R].Right == Null)
		printf("%d",R);
	AddQ(Q, R);//将根节点R插入队列
	while (!IsEmpty(Q)){
		R_1 = DeleteQ(Q);
		if ((++cnt) == N) flag = 0;
		if (T[R_1].Left == Null && T[R_1].Right == Null){
			printf("%d",R_1);
			if (flag) printf(" "); 
		}
		if (T[R_1].Left != Null) AddQ(Q, T[R_1].Left );
		if (T[R_1].Right != Null) AddQ(Q, T[R_1].Right );
	} 	
}

Queue CreateQueue(int MaxSize)
{
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType));
	Q->Front = Q->Rear = 0;
	Q->MaxSize = MaxSize;
	
	return Q;
 } 
 
int IsEmpty(Queue Q)
{
	if (Q->Front == Q->Rear) return 1;
	else return 0;
 } 
 
 void AddQ(Queue Q, ElementType X)
{
	Q->Rear = (Q->Rear+1) % Q->MaxSize ;
	Q->Data[Q->Rear ] = X;
 } 
 
ElementType DeleteQ(Queue Q)
{
	Q->Front = (Q->Front+1)% Q->MaxSize ;
	return Q->Data[Q->Front ];
 } 

测试点:最小N 未通过

猜你喜欢

转载自blog.csdn.net/qq_26565435/article/details/83242181