03-树2 List Leaves (25 分)

题目:

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

思路:
 用静态链表(即数组)的方式创建一棵树,但要注意要找到树的根结点,根结点的特性就是没有任何一颗树的孩子会是它,所以用一个数组sig来标记,一开始sig中的元素全为0,对于题目中出现的每个左孩子,右孩子,就把它在sig中对应的元素改为1,那么最后元素仍未0的就是根结点;之后采用层序遍历,将叶结点输出即可。

这里我采用链式存储来实现队列,这样代码好像有点长。。。。。。

还有,在创建一个队列时,一定要用malloc的方式申请内存之后在操作:

Queue Q = (Queue)malloc(sizeof(struct queue));
	Q->front = NULL;
	Q->rear = NULL;

 否则的话就会报段错误,哎,还是我对指针的理解不够透彻。。。。。

不管啦,就这样吧!!!

慢慢进升吧!!

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
#define Null -1
#define ERROR 0

struct TNode {                   //树结点
	int left;
	int right;
}T[MAXSIZE];
typedef struct QNode {           //队列结点
	int Data;
	struct QNode* Next;
}*QNodePtr;
typedef struct queue{            //队列
	QNodePtr front;
	QNodePtr rear;
}*Queue;

int CreateTree(struct TNode T[]);    //建树
void AddQ(Queue Q, int Elem);        //进队
int DeleteQ(Queue Q);                //出队
int IsEmpty(Queue Q);                //队是否为空

int main() {
	int Root = CreateTree(T);
	Queue Q = (Queue)malloc(sizeof(struct queue));
	Q->front = NULL;
	Q->rear = NULL;
	AddQ(Q, Root);        //根结点进队
	int bingo = 0;
	while (!IsEmpty(Q)) {
		int t = DeleteQ(Q);
		if (T[t].left != Null) AddQ(Q, T[t].left);     //左孩子进队
		if (T[t].right != Null) AddQ(Q, T[t].right);   //右孩子进队
		if (T[t].left == Null && T[t].right == Null) {  //没孩子的情况
			if (bingo) printf(" ");
			printf("%d", t);
			bingo = 1;
		}
	}

	return 0;
}
int CreateTree(struct TNode T[])
{
	int N;
	char cl, cr;
	int sig[MAXSIZE];
	scanf("%d\n",&N);
	for (int i = 0; i < N; i++) sig[i] = 0;  //sig最后用来找根结点
	for (int i = 0; i < N; i++) {
        scanf("%c %c\n",&cl,&cr);            //都以字符的方式读入
		if (cl != '-') {
			T[i].left = cl - '0';            //将字符转化为对应的数字
			sig[T[i].left] = 1;              //将sig中对应的0改为1
		}
		else T[i].left = Null;
		if (cr != '-') {
			T[i].right = cr - '0';
			sig[T[i].right] = 1;
		}
		else T[i].right = Null;
	}
	int t = 0;
	while (sig[t]) t++;           //遍历sig去找元素仍未0的下标,它就是根结点
	return t;
}
void AddQ(Queue Q, int Elem)
{
	QNodePtr QP = (QNodePtr)malloc(sizeof(struct QNode));
	QP->Data = Elem;
	QP->Next = NULL;
	if (Q->front == NULL) {
		Q->front = QP;
		Q->rear = QP;
	}
	else {
		Q->rear->Next = QP;
		Q->rear = QP;
	}
}
int DeleteQ(Queue Q)
{
	QNodePtr p;
	int Elem;
	if (Q->front == NULL) return ERROR;
	p = Q->front;
	if (Q->front == Q->rear) {
		Q->front = NULL;
		Q->rear = NULL;
	}
	else Q->front = Q->front->Next;
	Elem = p->Data;
	free(p);
	return Elem;
}
int IsEmpty(Queue Q)
{
	if (Q->front == NULL) return 1;
	else return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值