03-树1. List Leaves (25)

03-树1. List Leaves (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 20
int input(void){
	char c;
	do{
		c = getchar();
	}while(c != '-' && (c<'0' || c>'9'));
	if(c == '-')
		return -1;
	else
		return c-'0';	
}
struct node
{
	int left;
	int right;
};
int main(){
	void InOutQueue(int* p,struct node* tree,int* star,int* end);
	int num,i,head,j[20];
	int point=0;
	for(i=0;i<10;i++){j[i]=-1;} 
	char temp;
//	struct node *tree;
	scanf("%d",&num);
//	tree=(struct node *)malloc(num*sizeof(struct node));
	struct node tree[15];
	for(i=0;i<num;i++){
		tree[i].left= input();
		if(tree[i].left!=-1)
			j[tree[i].left]=1;
		tree[i].right= input();
		if(tree[i].right!=-1)
			j[tree[i].right]=1;	
		}
		
		//j[tree[i].right]=-1;
		
	

	for(i=0;i<num;i++){
		if(j[i]==-1){
			head=i;
			break;
		}
	}
	

	
	int queue[num+1],star = -1,end = 0;
	queue[0] = -1;
	queue[num] = -1;
	
	queue[end] = head;
	end++;

	while(1!= end -star){
		InOutQueue(queue,tree,&star,&end);
	}
	
}
void InOutQueue(int* p,struct node* tree,int* star,int* end){
	int t = p[(*star)+1];
	static int P = 0;
	if(tree[t].left!=-1){
		p[*end] = tree[t].left;
		(*end)++;
	}
	if(tree[t].right!=-1){
		p[*end] = tree[t].right;
		(*end)++;
	}
	if(tree[t].left == -1 && tree[t].right == -1){
		if(!P){
			printf("%d",t);
			P = 1;
		}else
			printf(" %d",t);
	}
	(*star)++;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值