首页 > 其他 > 详细

UVa 699 (二叉树) The Falling Leaves

时间:2014-09-23 00:32:14      阅读:231      评论:0      收藏:0      [点我收藏+]

题意:

按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和。

分析:

做了好几道二叉树的题,代码应该也很好理解了。这里maxn开始设了200、500都RE,后来索性开了2000,AC了

紫书上面init函数最后应该加一句 return true;

 

bubuko.com,布布扣
 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 2000;
 8 int sum[maxn];
 9 
10 void build(int p)
11 {
12     int v;
13     scanf("%d", &v);
14     if(v == -1)    return;
15     sum[p] += v;
16     build(p - 1);
17     build(p + 1);
18 }
19 
20 bool Init()
21 {
22     int v;
23     scanf("%d", &v);
24     if(v == -1)    return false;
25     memset(sum, 0, sizeof(sum));
26     int pos = maxn / 2;
27     sum[pos] = v;
28     build(pos - 1);
29     build(pos + 1);
30     return true;
31 }
32 
33 int main(void)
34 {
35     #ifdef LOCAL
36         freopen("699in.txt", "r", stdin);
37     #endif
38 
39     int kase = 1;
40     while(Init())
41     {
42         int p = 0;
43         while(sum[p] == 0) p++;
44         //if(kase > 1)    printf("\n");
45         printf("Case %d:\n%d", kase++, sum[p++]);
46         while(sum[p] != 0)    printf(" %d", sum[p++]);
47         printf("\n\n");
48     }
49 
50     return 0;
51 }
代码君

 

UVa 699 (二叉树) The Falling Leaves

原文:http://www.cnblogs.com/AOQNRMGYXLMV/p/3987340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!