PTA_2019春_032_List Leaves

/*List Leaves*/
/*思路:遍历树找出叶节点,然后对树进行层序遍历,按层序遍历顺序输出叶节点即可*/
#include<stdio.h>
#include<stdlib.h>
typedef struct tree {
	int left;
	int right;
}Tree;
/*队列数组*/
typedef struct Qnode* ptrtoQnode;
struct Qnode {
	int* data; 
	int front, rear;/*队列头尾指针*/
	int maxsize;/*容量*/
};
typedef ptrtoQnode Queue;

/*以下几个函数为队列操作集*/
Queue CreateQueue(int maxsize);
int Isfull(Queue Q);
void  AddQ(Queue Q, int x);
int Isempty(Queue Q);
int DeleteQ(Queue Q);

int main() {
	int N;/*节点数*/
	char laji;/*用来除去\n*/
	scanf("%d", &N);
	Tree* tree = (struct tree*)malloc(N * sizeof(struct tree));
	for (int i = 0; i < N; i++) {
		scanf("%c",&laji);
		if (!scanf("%d", &tree[i].left)) tree[i].left = -1;    /*空子节点为-1*/
		if (!scanf("%d", &tree[i].right)) tree[i].right = -1;
	}/*输入完成*/
	/*测试输出*/
/*
    for(int i =0;i<N;i++){
    	printf("%d %d\n",tree[i].left,tree[i].right);
	}
*/

	/*找根节点*/
	int root;
	int* array = (int*)malloc(N * sizeof(int));/*标记数组,辅助作用*/
	for (int i = 0; i < N; i++)  array[i] = 1;/*全部设为1*/
	for (int i = 0; i < N; i++) {             /*不是根节点设为0*/
		if (tree[i].left!=-1) array[tree[i].left] = 0;
		if (tree[i].right!=-1) array[tree[i].right] = 0;
	}
	for (int i = 0; i < N; i++) {
		if (array[i]) {
			root = i;
			break;
		}
	}/*寻找完毕*/

	/*找叶节点*/
	int numberofleaves=0;
	for (int i = 0; i < N; i++) {
		if (tree[i].left == -1 && tree[i].right == -1){
			array[i] = -1;     /*叶节点标记为-1*/
			numberofleaves++;
	}
	}/*over*/
/*测试array数组*/
/*
    for(int i = 0;i<N;i++) {
    	printf("%d\n",array[i]);
	}
*/
	/*层序遍历,并输出*/
	Queue Q;
	int k;
	Q = CreateQueue(11);
	AddQ(Q, root);
	while (!Isempty(Q)) {
		k = DeleteQ(Q);
		if (array[k] == -1) {
			printf("%d", k);
			numberofleaves--;
			if (numberofleaves != 0)  printf(" ");
		}
		if (tree[k].left != -1)  AddQ(Q, tree[k].left);
		if (tree[k].right != -1)  AddQ(Q, tree[k].right);

	}

	return 0;
}
/*队列*/
Queue CreateQueue(int maxsize) {
	Queue Q = (Queue)malloc(sizeof(struct Qnode));
	Q->data = (int*)malloc(maxsize * sizeof(int));
	Q->front = Q->rear = 0;
	Q->maxsize = maxsize;
	return Q;
}
int Isfull(Queue Q) {
	return((Q->rear + 1) % Q->maxsize == Q->front);
}
void  AddQ(Queue Q, int x) {
	Q->rear = (Q->rear + 1) % Q->maxsize;
	Q->data[Q->rear] = x;
}
int Isempty(Queue Q) {
	return(Q->rear == Q->front);
}
int DeleteQ(Queue Q) {
	Q->front = (Q->front + 1) % Q->maxsize;
	return Q->data[Q->front];
}

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:

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

Sample Output:

4 1 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值