7-4 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.

Sample Input:

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

Sample Output:

4 1 5

 

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 100

//树结点结构 
typedef struct TNode{
	int Left;  //指向左子树的指针
	int Right; 
}Tree;

//循环队列元素结构 
struct QNode {
    int *Data;     /* 存储元素的数组 */
    int Front, Rear;       /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;

void LevelOrderTraversal(Tree a[],int Root);

int main()
{
	int N;
	char u,v;
	int son[15] = {0};  //将会用来判断哪个结点是根结点 
	int root;  //二叉树的根结点 
	Tree a[15] = {-1};
	
	scanf("%d",&N);
	getchar(); //吸收掉换行符 
	for(int i=0; i<N; i++){
		scanf("%c %c",&u,&v);
		if(u == '-'){
			u = -1;  //没有儿子,指针设为-1
		}else{
			u = u-'0';
			son[u] = 1; //有指针指向的结点、在son[]数组里设为1 
		}
		if(v == '-'){
			v = -1;  //没有儿子,指针设为-1
		}else{
			v = v-'0';
			son[v] = 1;
		}
		a[i].Left = u;  //数组的下标代表了这个结点的指针 
		a[i].Right = v;
		getchar();  //吸收掉换行符 
	}
    /*寻找根结点*/ 
	for(root=0; root<N && son[root]==1; root++);   
	LevelOrderTraversal(a,root);
	
	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;
}

//判队循环队列空 
bool IsEmptyQ( Queue Q )
{
    return (Q->Front == Q->Rear);
}

//判队循环队列满 
bool IsFullQ( Queue Q )
{
    return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}

//循环队列的插入 
bool AddQ( Queue Q, int X )
{
    if ( IsFullQ(Q) ) {
        printf("队列满\n");
        return false;
    }
    else {
        Q->Rear = (Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear] = X;
        return true;
    }
}

//循环队列的删除
int DeleteQ( Queue Q )
{
    if ( IsEmptyQ(Q) ) { 
        printf("队列空\n");
        //return;
    }else{
        Q->Front = (Q->Front+1)%Q->MaxSize;
        return  Q->Data[Q->Front];
    }
}

/********层序遍历*******/
void LevelOrderTraversal(Tree a[],int Root)
{
	Queue Q;
	int r,ans = 1;
	if( Root == -1 )  return;  //如果Root为-1,说明不存在根结点,则直接返回 
	Q = CreateQueue(MAXSIZE);  //创建并初始化队列 Q
	AddQ(Q,Root);
	while( !IsEmptyQ(Q) ){
		r = DeleteQ(Q);  
		if(a[r].Left == -1 && a[r].Right == -1){  /*访问取出队列的结点*/ 
			if(ans){
				printf("%d",r);
				ans = 0;
			}else{
				printf(" %d",r);
			}
		}
		if(a[r].Left != -1)  AddQ(Q,a[r].Left);    //访问左右儿子 
		if(a[r].Right != -1)  AddQ(Q,a[r].Right);
	}
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值