// UVa699 The Falling Leaves

// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空

// UVa699 The Falling Leaves
// Rujia Liu
// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空树
// 算法:在“建树”的同时计算,无须真正的把树保存下来 #include<cstring>
#include<iostream>
using namespace std; const int maxn = 200;
int sum[maxn]; // 输入并统计一棵子树,树根水平位置为p
void build(int p) {
int v;
cin >> v;
if(v == -1) return; // 空树
sum[p] += v;
build(p - 1);
build(p + 1);
} // 边读入边统计
bool init() {
int v;
cin >> v;
if(v == -1) return false; memset(sum, 0, sizeof(sum));
int pos = maxn/2; // 树根的水平位置
sum[pos] = v;
build(pos - 1); // 左子树
build(pos + 1); // 右子树
return true;
} int main() {
int kase = 0;
while(init()) {
int p = 0;
while(sum[p] == 0) p++; // 找最左边的叶子 // 开始输出。因为要避免行末多余空格,所以稍微麻烦一点
cout << "Case " << ++kase << ":\n" << sum[p++];
while(sum[p] != 0) {
cout << " " << sum[p];
p++;
}
cout << "\n\n";
}
return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; struct Node
{
Node* l;
Node* r;
int v;
int index;
}; Node* root; int min_index;
int max_index;
int ans[10000]; Node* dfs(int index)
{
int i;
cin>>i;
//cout<<i<<endl;
if(i==-1) return 0;
Node* root=new Node;
root->v=i;
root->index=index;
if(index<min_index)
min_index=index;
if(index>max_index)
max_index=index;
root->l=dfs(index-1);
root->r=dfs(index+1);
return root;
} void delete_tree(Node* root)
{
if(!root)
return;
delete_tree(root->l);
delete_tree(root->r);
delete root;
} void bfs()
{
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* nd=q.front(); q.pop();
ans[nd->index - min_index]+=nd->v; //cout<<nd->v<<" ";
if(nd->l) q.push(nd->l);
if(nd->r) q.push(nd->r);
} } void output()
{
int i;
for(i=0;i<max_index-min_index;i++)
{
cout<<ans[i]<<" ";
}
cout<<ans[i]<<endl<<endl;;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva699.in", "r", stdin);
#endif
int kase=1;
while(root=dfs(0))
{
cout<<"Case "<<kase++<<":"<<endl;
bfs();
output();
delete_tree(root);
min_index=max_index=0;
memset(ans, 0, sizeof(ans));
} return 0;
}

UVa699 The Falling Leaves的更多相关文章

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

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

  2. H - The Falling Leaves

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

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

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

  4. POJ 1577 Falling Leaves 二叉搜索树

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

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

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

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

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

  7. The Falling Leaves(建树方法)

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

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

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

  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. I.MX6 lcd lvds hdmi bootargs

    /********************************************************************* * I.MX6 lcd lvds hdmi bootarg ...

  2. java 错误:找不到或无法加载主类的解决办法

    此类错误的常见解决办法: 1.是因为.java文件不在项目的src路径内,也就是说源代码未被eclipse编译,字节码不存在无法运行了在项目名上右键 -> Builder Path -> ...

  3. db2数据库Date相关函数

    1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2' FROM SYSIBM ...

  4. z-index的妙用

    总是在纠结一个问题,当然我是前端初学者.这个问题就是,一个元素放在另一个元素里面,总希望它显示时,但是别撑开元素.这个时候一定要想到z-index. 例如今天写的一个浮动在导航栏下面的一个图片,我用的 ...

  5. 利用反射自动生成SQL语句(仿Linq)

    转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...

  6. hashCode之一--两个对象值相同,有相同的hash code

    两个对象值相同(x.equals(y) == true),则一定有相同的hash code. 这是java语言的定义:  因为:Hash,一般翻译做“散列”,也有直接音译为"哈希" ...

  7. Json::Value使用心得

    Json::Value 是sourceforge开源项目jsoncpp的数据对象,用来处理json数据  下载 1.打印Json数据 Json::Value jv; Json::FastWriter ...

  8. hdu1998 bjfu1272奇数阶幻方构造

    这题就是一个sb题,本来很水,硬是说得很含混.奇数阶幻方构造其实有好多方法,这题既不special judge,也不说清楚,以为这样能把水题变成难题似的,简直想骂出题人. /* * Author : ...

  9. Asp.Net中文本换行

    Asp.Net中文本换行 VB.NET Function HtmlCode(ByVal fString)        If fString <> "" Then    ...

  10. dwr消息推送

    闲来无事,把自己关于对dwr消息推送的实现过程描述一番. 首先第一步,当然在工程中是加入dwr.jar了,接着在web.xml中配置以下代码 <servlet> <servlet-n ...