03-树2 List Leaves (25 分

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

#include<stdio.h>
#include<stdlib.h>
typedef struct tree {
	int l;//代表自己的所对应的数组下标
	int left;//代表左儿子的数组下标
	int right;/代表右儿子的数组下标
}tree;
tree at[10];
//tree bt[10];
int  buildtree(tree a[],int N)将输入储存到结构体树数组中 并找出其根节点对应数组下标
{
	int i,check[10];
	int root;
	char right, left;
	if (N == 0) { root= -1; return root; }
	for (i = 0; i < N; i++)check[i] = 0;//判断根节点先初始话为0;每当输入一个一个子树下标时,将对应下标的数组值置为1
	for (i = 0; i < N; i++)
	{
		scanf("%c %c",&left,&right);
		
		getchar();
		a[i].l = i;
		if (left != '-')
		{
			a[i].left = left - '0';
			check[a[i].left] = 1;
		}
		else a[i].left = -1;
		if (right !='-')
		{
			a[i].right=right - '0';
			check[a[i].right] = 1;
		}
		else a[i].right = -1;
	}
	for (i = 0; i < N; i++)
		if (check[i] == 0) root = i;
	return root;
}
//int isomorphism(int a, int b)
//{
//	if (a == -1 && b == -1)return 1;//both empty
//	if (a == -1 && b != -1 || b == -1 && a != -1)return 0;//one of them emty
//	if (at[a].l != bt[b].l)return 0;root different
//	if ((at[a].left == -1) && (bt[b].left == -1))//两边左节点都空,判断右节点
//		return isomorphism(at[a].right, bt[b].right);
//	if (((at[a].left != -1) && (bt[b].left != -1)) && ((at[at[a].left].l) == (bt[bt[b].left].l)))//两边左节点都不空且值相等,判断右节点和左节点
//		return (isomorphism(at[a].right, bt[b].right) && isomorphism(at[a].left, bt[b].left));
//	else return (isomorphism(at[a].left, bt[b].right) && isomorphism(at[a].right, bt[b].left));//两节左点不相等,交换左右节点再此判段
//
//}
void print(int aroot,int n)
{
	int q[10];
	int i,j,x=0;x用来格式输出
	q[0] = at[aroot].l;///先将根节点的值,即at数组下标值赋给q[0]
	int lnode;
	for (i = 0,j=0;i <n-1;)/用i代表队头,j代表队尾
	{
		lnode = q[j++];
		层序遍历往队列里先存左子树再存右子树
		if (at[lnode].left != -1) q[++i] = at[at[lnode].left].l;
		if (at[lnode].right != -1) q[++i] =at[at[lnode].right].l;

	}
	for (i = 0; i < n; i++)
	{
		if (at[q[i]].left == -1 && at[q[i]].right == -1)当该节点没有子节点时就为叶节点
		{
			if (x)printf(" ");//
			printf("%d", q[i]);
			x++;
		}
	}
}
int main()
{
	int  aroot;
	int N;
	scanf("%d", &N);
	getchar();
	 aroot= buildtree(at,N);
	 //broot= buildtree(bt);
	 /*if(isomorphism(aroot, broot)) printf("Yes");
	 else printf("No");*/
	 print(aroot,N);

	 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值