UVA 699 The Falling Leaves

UVA 699 The Falling Leaves

题意 输入一棵树,把垂直方向上同一位置的叶子节点值加以来,最后逐个输出

思路 题目中是按前序输入树,所以就按前序来建立一棵树,之后建一个数组,从数组中间开始存,每次访问左节点就把数组往左存,访问右节点就往右,最后输出储存的数组即可

代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int num[1000];
int output[1000];
typedef struct node  
{   
    int t;
    int data; 
    struct node *rchild; 
    struct node *lchild; 
} BTNode; 

BTNode *create(int tt) 
{ 
    BTNode *root; 
    int ro; 
    scanf("%d",&ro); 
    if(ro == -1) 
	root = NULL; 
    else
    { 
	num[tt] += ro;
	root = (BTNode *)malloc(sizeof(BTNode)); 
	root -> data = ro;
	root -> lchild=create(tt - 1); 
	root -> rchild=create(tt + 1); 
    } 
    return root; 
} 

int main() 
{
    int ttt = 1;
    while (1)
    { 
	BTNode *t;
	int j = 0;
	memset(num, 0, sizeof(num));
	t = create(500); 
	if (t == NULL)
	    break;
	for (int i = 0 ; i < 1000; i ++)
	{
	    if (num[i])
		output[j++] = num[i];
        }
	printf("Case %d:\n", ttt ++);
	for (int i = 0; i < j - 1; i ++)
	    printf("%d ", output[i]);
	printf ("%d\n", output[j - 1]);
	printf("\n");
    }
    return 0; 
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值