Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the
trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened
to binary trees, how large would the piles of leaves become?
We assume each node in a binary tree ”drops” a number of leaves equal to the integer value stored
in that node. We also assume that these leaves drop vertically to the ground (thankfully, there’s no
wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a
manner that the left and right children of a node are exactly one unit to the left and one unit to the
right, respectively, of their parent. Consider the following tree on the right:
The nodes containing 5 and 6 have the same horizontal position
(with different vertical positions, of course). The node
containing 7 is one unit to the left of those containing 5 and
6, and the node containing 3 is one unit to their right. When
the ”leaves” drop from these nodes, three piles are created:
the leftmost one contains 7 leaves (from the leftmost node),
the next contains 11 (from the nodes containing 5 and 6), and
the rightmost pile contains 3. (While it is true that only leaf
nodes in a tree would logically have leaves, we ignore that in
this problem.)
Input
The input contains multiple test cases, each describing a single tree. A tree is specified by giving the
value in the root node, followed by the description of the left subtree, and then the description of the
right subtree. If a subtree is empty, the value ‘-1’ is supplied. Thus the tree shown above is specified
as ‘5 7 -1 6 -1 -1 3 -1 -1’. Each actual tree node contains a positive, non-zero value. The last test
case is followed by a single ‘-1’ (which would otherwise represent an empty tree).
Output
For each test case, display the case number (they are numbered sequentially, starting with 1) on a line
by itself. On the next line display the number of “leaves” in each pile, from left to right, with a single
space separating each value. This display must start in column 1, and will not exceed the width of an
80-character line. Follow the output for each case by a blank line. This format is illustrated in the
examples below.
Sample Input
5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1
Sample Output
Case 1:
7 11 3
Case 2:
9 7 21 15

就是给你棵树,让你输出竖着的每一排节点的值的和

代码如下:

 #include <bits/stdc++.h>

 using namespace std;
int sum[];
void build (int pos)
{
int elem;
scanf("%d",&elem);
if (elem!=-)
{
sum[pos]+=elem;
build(pos-);
build(pos+);
}
}
int main()
{
int root,casee=;
bool f=false;
//freopen("de.txt","r",stdin);
while (~scanf("%d",&root))
{
memset(sum,,sizeof sum);
if (root==-)
break;
f=true;
printf("Case %d:\n",++casee);
sum[]=root;
build();
build();
queue<int>q;
for (int i=;i<;++i)
{
if(sum[i]!=)
q.push(sum[i]);
}
bool f2=false;
while (!q.empty())
{
if (f2)
printf(" ");
f2=true;
printf("%d",q.front());
q.pop();
}
printf("\n\n");
}
return ;
}

各种PE23333333333

UVa 699 The Falling Leaves (树水题)的更多相关文章

  1. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  2. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  3. UVA 699 The Falling Leaves (二叉树水题)

    本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...

  4. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

  5. uva 699 The Falling Leaves(建二叉树同一时候求和)

    本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开 ...

  6. UVA 699 The Falling Leaves (递归先序建立二叉树)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/19244 #include <iostream> #include <cstdio> # ...

  7. uva 699 the falling leaves——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3QAAAMsCAIAAACTL3d2AAAgAElEQVR4nOx9y7GuPA4tKRADk/92T8 ...

  8. UVa 699 The Falling Leaves

    题意:给出按先序输入的一颗二叉树,分别求出从左到右的相同横坐标上的节点的权值之和 递归建树,然后用sum数组分别统计每一个横坐标上的权值之和 感觉建树都在递归递归递归= =慢慢理解吧 #include ...

  9. POJ 3468 A Simple Problem with Integers(线段树水题)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 135904 ...

随机推荐

  1. 在Developerkit开发板上运行blink例程

    本文将介绍怎么样在VScode环境下,将AliOS Tings提供的blink例程在Developerkit开发板上运行起来. DeveloperKit开发板   在例程中分别用到两个led和一个按钮 ...

  2. 4412 make menuconfig和make

    一.Menuconfig的操作 • Linux编译器通过.config文件确认哪些代码编译进内核,哪些被裁减掉• menuconfig是生成.config的一个工具• 在Linux发展过程中,配置内核 ...

  3. 洛谷 4099 [HEOI2013]SAO——树形DP

    题目:https://www.luogu.org/problemnew/show/P4099 结果还是看了题解才会…… 关键是状态,f[ i ][ j ] 表示 i 子树. i 号点是第 j 个出现的 ...

  4. HihoCoder 1055 刷油漆 (树上背包)

    题目:https://vjudge.net/contest/323605#problem/A 题意:一棵树,让你选择m个点的一个连通块,使得得到的权值最大 思路:树上背包,我们用一个dp数组,dp[i ...

  5. Sqlachemy的警告SAWarning: The IN-predicate on "sns_object.BIZ_ID" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.

    我在使用db_session.query,查询的时候idlist是个空值时候,执行下面的语句就会出现警告.其中后面delete(synchronize_session=False)是删除前面的一堆查询 ...

  6. (60)c# com com+ dcom

    一.创建COM组件 1.创建项目 2.设置使程序集可见 AssemblyInfo.cs中flase改为ture 或者 项目属性 设置可见 3.Guid生成器 在开始程序下能够找到Guid生成器 每次都 ...

  7. 搭建干净的Mac开发学习环境

    docker + linux + gcc/g++ https://www.jianshu.com/p/d113db99fe24 https://www.jianshu.com/p/d26140d20c ...

  8. c# 编程--结构体

    结构体:由多种简单类型,组合成一种复杂的类型.使用这种复杂的类型来解决生活中的实际例子. 一.结构体定义:struct 结构体的名子{    public 类型名 变量名;    .....}    ...

  9. javascript基础7(正则及表单验证)

    1.正则的概念     JS诞生的目的是什么?     就是为了做表单验证.       在JS未出现以前,表单的信息验证需要传输给后台,让后台做数据验证处理之后,再返回给前端页面处理的结果.在带宽有 ...

  10. css 设置div基于父元素宽度的宽高相等的样式

    1. 前言 在移动开发中,有时候需要设置一个宽高相等的div,并且为了使它能够适配更多的屏幕,于是需要让它的宽高和屏幕宽高成一定的比例.这里将提供一个css的解决方案,让一些后端开发不用再写繁琐的js ...