UVA 399 The Falling Leaves(二叉树)

 The Falling Leaves 

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<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef struct Node{
    int data;
    struct Node *lchild;
    struct Node *rchild;
}Node;
Node *T;
int num[550];
int x,n;
Node *CreatTree(int n)
{
    if(n==-1)
        return NULL;
    Node *t=(Node*)malloc(sizeof(Node));
    t->data=n;
    t->lchild=t->rchild=NULL;
    cin>>x;
    t->lchild=CreatTree(x);
    cin>>x;
    t->rchild=CreatTree(x);
    return t;
}
void dfs(Node *t,int cnt)
{
    if(t==NULL)  return ;
    num[cnt]+=t->data;
    dfs(t->lchild,cnt-1);
    dfs(t->rchild,cnt+1);
}
int main()
{
    int cas=0;
    while(cin>>n&&n!=-1)
    {
        CLEAR(num,0);
        T=CreatTree(n);
        dfs(T,50);
        int flag=0;
        cout<<"Case "<<++cas<<":"<<endl;
        for(int i=0;i<100;i++)
        {
            if(num[i])
            {
                if(!flag)  cout<<num[i];
                else   cout<<" "<<num[i];
                flag=1;
            }
        }
        cout<<endl<<endl;
    }
    return 0;
}
时间: 2024-07-29 14:20:18

UVA 399 The Falling Leaves(二叉树)的相关文章

POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树. 都是两个基本操作解决: 1 二叉树插入操作 2 前序遍历 简单题目了. #include <stdio.h> #include <string> #include <algorithm> #include <vector> using std::vector; using std::string; const int MAX_B = 1024; char buf[MAX_B]; int

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

本文纯属原创,转载请注明出处,谢谢.http://blog.csdn.net/zip_fan. 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 sam

UVa 699.The Falling Leaves【7月23】

The Falling Leaves 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 lar

UVA 699 The Falling Leaves

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<map> #include<algorithm> #include<cctype>

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s

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

本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开一个数组.这里我是把根节点作为第42个数,能够在建树的同一时候求 出那一列全部数值的和左孩子节点减一,右孩子节点加一.. .写的时候中间出了点小bug,忘了给flag重置0了,调 了好久.. . 第一次提交wa了,由于没有换行,题目要求结果之间有一行空行的. . . 贴代码: #include<st

[2016-02-09][UVA][699][The Falling Leaves]

时间:2016-02-09 13:29:10 星期二 题目编号:UVA 699 题目大意:  给一棵树,每棵树有一个叶子,叶子的值是点权,求叶子垂直落下后, (同一个方向的形成一堆),求每堆叶子的总权值 位置的描述:每个左子树在根左边一个单位,右子树在根右边一个单位 分析: 遍历一遍二叉树,传参保存每个二叉树的位置,最后保存即可 每行不超过80个字符,那么节点数不大于80个,堆数不大于80个 方法:dfs,递归 解题过程遇到问题: 一行数据不一定就是一颗完整的树!!!!!!! 开始还以为读取到第

POJ 1577 Falling Leaves 二叉树操作

Description Figure 1 Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and

uva 699 the falling leaves——yhx

因为复制过来排版很乱,所以上截图. 1 #include<cstdio> 2 #include<cstring> 3 int sum[10010]; 4 void bd(int p) 5 { 6 int x; 7 scanf("%d",&x); 8 if (x==-1) return; 9 sum[p]+=x; 10 bd(p-1); 11 bd(p+1); 12 } 13 bool dl() 14 { 15 int i,j,k,p,q,x,y,z; 1