UVA 699(二叉树建树与遍历)

M - The Falling Leaves

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Appoint description: 
System Crawler  (2014-02-08)

Description

 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

Miguel Revilla

2000-08-14

给出一颗树,让所有的节点垂直下落,计算每堆节点的值之和

#include<stdio.h>
#include<string.h>
int ans[10005],A;
struct tree
{
    int lca,date;
    tree *l,*r;
    tree()
    {
        lca=0;
        l=r=NULL;
    }
};
tree *build(int num,int ok)
{
    if(ok)scanf("%d",&A);
    if(A==-1)return NULL;
    tree *p=new tree;
    p->date=A;
    p->lca=num;
    p->l=build(num-1,1);
    p->r=build(num+1,1);
    return p;
}
void dfs(tree *p)
{
    if(!p)return ;
    ans[p->lca]+=p->date;
    p->date=0;
    dfs(p->l);
    dfs(p->r);
}
int cas=1;
int main()
{
    //freopen("in.txt","r",stdin);
    tree *root=NULL;
    while(~scanf("%d",&A))
    {
        if(A==-1)return 0;
        root=NULL;
        root=build(5000,0);
        memset(ans,0,sizeof(ans));
        dfs(root);
        printf("Case %d:\n",cas++);
        int i;
        for(i=0;i<=10000;i++)
        if(ans[i]){printf("%d",ans[i]);break;}
        for(i+=1;i<=10000;i++)
        if(ans[i])printf(" %d",ans[i]);
        printf("\n\n");
    }
    return 0;
}

UVA 699(二叉树建树与遍历),布布扣,bubuko.com

时间: 2024-12-14 12:51:25

UVA 699(二叉树建树与遍历)的相关文章

UVa 548 (二叉树的递归遍历) Tree

题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍历,先父节点  然后左孩子  最后右孩子 中序遍历,先左孩子  然后父节点  最后父节点 后序遍历,先左孩子  然后右孩子  最后父节点 这里有更详细的解释: http://blog.csdn.net/sicofield/article/details/9066987 紫书上面写错了,后序遍历最后一

UVa 548 Tree(建树,递归遍历)

题意  给你一个树的中序遍历和后序遍历  某个节点的权值为从根节点到该节点所经过节点的和  求权值最小的叶节点的值  如果存在多个  输出值最小的那个 把树建好就好说了  递归递归dfs msun保存最小叶节点权值  ans保存答案 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 10005, INF = 0x3f3f3f3f; int i

UVa 699 (二叉树) The Falling Leaves

题意: 按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和. 分析: 做了好几道二叉树的题,代码应该也很好理解了.这里maxn开始设了200.500都RE,后来索性开了2000,AC了 紫书上面init函数最后应该加一句 return true; 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

二叉树的递归遍历 The Falling Leaves UVa 699

题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与天平那题不同,本题不需要构造出完整的结点左右子树,只需要构造出结点的相对位置,每次输入一个结点树,若为-1,则返回,否则依次递归执行input(p-1)与input(p+1). 代码如下: 1 #include<stdio.h> 2 #include<cstring> 3 #inclu

UVA 548(二叉树重建与遍历)

J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-16) Description  Tree  You are to determine the value of the leaf node in a given binary tree that is the ter

[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversa

数据结构之 二叉树---求二叉树后序遍历和层次遍历(先建树,再遍历)

数据结构实验之求二叉树后序遍历和层次遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历. 输入 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据.每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列. 输出 每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列 示例输入 2 abdegcf dbgeaf

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

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

[数据结构]二叉树创建与遍历

实验报告:二叉树创建与遍历 一.问题描述 二叉树是一种实用范围很广的非线性结构,一棵非空二叉树有也只有一个根结点,每个结点最多有两个子树,我们称为左子树与右子树,当一个结点的左.右子树都是空的时,沃恩称此结点为叶子结点. 二叉树有一些很好的性质,这里不再赘述.考虑如何存储一棵树,本实验选择使用链式存储结构——二叉链表:如果事先知道需要存储的二叉树是满二叉树或者完全二叉树,则可以考虑使用顺序存储,否则将浪费大量的存储空间. 对于一棵既成的二叉树,有三种遍历方式——先序.中序与后序.可以证明,一棵形