uva 699

紫书P159

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 <cstring>
using namespace std;
const int maxn=;
int sum[maxn]; void Build(int u)
{
int v;
cin>>v;
if(v==-) return;
sum[u]+=v;
Build(u-);
Build(u+);
} bool Init()
{
memset(sum,,sizeof(sum));
int v;
cin>>v;
if(v==-) return false;
int pos=maxn/;
sum[pos]=v;
Build(pos-);
Build(pos+);
} int ca=;
void Output()
{
int p=;
while(sum[p]==) p++;
cout<<"Case "<<ca++<<":\n"<<sum[p];
while(sum[++p]!=) {cout<<" "<<sum[p];}
cout<<"\n\n";
} int main()
{
while(Init())
{
Output();
}
return ;
}

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. 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. POJ 1577 Falling Leaves 二叉搜索树

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

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

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

  7. H - The Falling Leaves

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

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

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

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

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

随机推荐

  1. 配置URL

  2. bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机【贪心+差分】

    首先O(n^3)的贪心很好想,就是枚举k然后从前往后扫,扫到反就翻转区间 然后考虑优化掉翻转区间维,就是搞成差分的形式,在翻转区间的尾部打上标记,再用一个变量维护当前的翻转次数,加到当前状态上来判断是 ...

  3. Syntax error on token ";", , expected 错误

    eclipse错误提示如图: 错误代码如图: 一开始百思不得其解,后来终于发现问题的原因所在,java中变量的声明可以不在方法中,但语句只能出现在方法中,可以再声明变量的时候就赋初值,但如果要单独赋值 ...

  4. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  5. ACM_区间调度问题(贪心)

    Meetings 系列一 Time Limit: 2000/1000ms (Java/Others) Problem Description: 多年之后的广财ACM编协如日中天,下系多个部门,且编协成 ...

  6. 403 Frog Jump 青蛙过河

    一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...

  7. 项目需求会__前端er定位的思考~

    一.页面展示-----针对前端部分:后台的东西(功能.样式)不考虑! 二.动态效果------能不能实现! 三.接口数据------怎么传数据! 四.兼容性--------兼容到哪个版本浏览器! 五. ...

  8. sql server 行转列 要注意的问题 pivot

      select * from (  select mvqr.VoteQuestionId,mvqr.AnswerSolution from  JY_MemberVoteQuestionRef as  ...

  9. [ Luogu 3927 ] Factorial

    \(\\\) \(Description\) 求 \(N!\) 在 \(K\) 进制表示下末尾 \(0\) 的个数. \(N,K\in [1,10^{12}]\) \(\\\) \(Solution\ ...

  10. 如何取消android studio启动时自动打开上次关闭的项目

    Androidstudio默认每次android studio启动就会自动打开上次关闭的项目,如果想要取消并让它显示此界面 只需要