Description

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:

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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int mod=;
const int MS=; int sum[MS];
void build(int p)
{
int v;
cin>>v;
if(v==-)
return ;
sum[p]+=v;
build(p-);
build(p+);
}
bool init()
{
int v;
cin>>v;
if(v==-)
return false;
int p=MS/;
memset(sum,,sizeof(sum));
sum[p]+=v;
build(p-);
build(p+);
return true;
} int main()
{
int kase=;
while(init())
{
int p=;
while(sum[p]==)
p++;
cout<<"Case "<<++kase<<":"<<endl<<sum[p++];
while(sum[p])
cout<<" "<<sum[p++];
cout<<endl<<endl;
}
return ;
}
												

H - The Falling Leaves的更多相关文章

  1. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  2. UVa 699 The Falling Leaves (树水题)

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

  3. UVa699 The Falling Leaves

      // UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...

  4. UVA - 699The Falling Leaves(递归先序二叉树)

    The Falling Leaves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

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

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

  6. 【数据结构】The Falling Leaves(6-10)

    [UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...

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

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

  8. The Falling Leaves(建树方法)

    uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...

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

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

随机推荐

  1. Java 类型信息

    <Thinking in Java 4th>第14章 类型信息 运行时类型信息(Run-Time Type Identification)使得你可以在程序运行时发现和使用类型信息. 14. ...

  2. <转载>gcc/g++编译

    转载于:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/22/1782678.html 1. gcc/g++在执行编译工作的时候,总共需要4步 ...

  3. MS 数据库存储过程加密解密

    存储过程加密解密在网上有很多,刚刚好最近需要用到,所以就查询了一下资料.记录一下 加密方法:执行如下存储过程 DECLARE @sp_name nvarchar(400) DECLARE @sp_co ...

  4. 安装php5.5

    安装php5.5 ./configure --prefix=/usr/local/php5.5.14/ --with-apxs2=/usr/local/apache2.2.27/bin/apxs -- ...

  5. hdu 5510 Bazinga

    http://acm.hdu.edu.cn/showproblem.php?pid=5510 Problem Description: Ladies and gentlemen, please sit ...

  6. git 安装与使用场景

    1. 安装 yum install git #自动安装依赖 centos sudo apt-get install git #ubutu http://msysgit.github.io/ #wind ...

  7. Mahout之Navie Bayesian命令端运行

    landen@landen-Lenovo:~/文档/20news$ mahout trainclassifier --helpMAHOUT_LOCAL is not set; adding HADOO ...

  8. 使用VS2013调试FluorineFx程序

    VS2013,建立 FluorineFx Web 项目方法: 先新建.项目.Web.选择.NET 3.5 ASP.NET 窗体程序来新建一个项目.复制 log.Templates.WEB-INF 文件 ...

  9. C:结构体

    结构体 构造类型:就是有基本的类型组成的 1.结构体  结构体是一种自定义的数据类型 和 int float 是一样的都可以定义变量 数组 只能存放一种类型的容器 结构体   可以存放多种数据类型  ...

  10. [Jobdu] 题目1497:面积最大的全1子矩阵

    题目描述: 在一个M * N的矩阵中,所有的元素只有0和1,从这个矩阵中找出一个面积最大的全1子矩阵,所谓最大是指元素1的个数最多. 输入: 输入可能包含多个测试样例.对于每个测试案例,输入的第一行是 ...