题目链接:http://acm.hust.edu.cn/vjudge/problem/19244

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int maxn = ;
int sum[maxn];
// 输入并统计一棵子树,树根水平位置为p
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;
memset(sum, , sizeof(sum));
int pos = maxn/; // 树根的水平位置
sum[pos] = v;
build(pos - ); // 左子树
build(pos + ); // 右子树
return true;
}
int main() {
int kase = ;
while(init()) {
int p = ;
while(sum[p] == ) p++; // 找最左边的叶子
// 开始输出。因为要避免行末多余空格,所以稍微麻烦一点
cout << "Case " << ++kase << ":\n" << sum[p++];
while(sum[p] != ) {
cout << " " << sum[p];
p++;
}
cout << "\n\n";
}
return ;
}

UVA 699 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. UVA 699 The Falling Leaves (二叉树水题)

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

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

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

  5. uva 699 the falling leaves——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3QAAAMsCAIAAACTL3d2AAAgAElEQVR4nOx9y7GuPA4tKRADk/92T8 ...

  6. UVa 699 The Falling Leaves

    题意:给出按先序输入的一颗二叉树,分别求出从左到右的相同横坐标上的节点的权值之和 递归建树,然后用sum数组分别统计每一个横坐标上的权值之和 感觉建树都在递归递归递归= =慢慢理解吧 #include ...

  7. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

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

    本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开 ...

  9. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

随机推荐

  1. 洛谷P4592 [TJOI2018]异或 【可持久化trie树】

    题目链接 BZOJ4592 题解 可持久化trie树裸题 写完就A了 #include<algorithm> #include<iostream> #include<cs ...

  2. 【BZOJ 2753 滑雪与时间胶囊】

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2843  Solved: 993[Submit][Status][Discuss] Descripti ...

  3. 【BZOJ 4103】 [Thu Summer Camp 2015]异或运算 可持久化01Trie

    我们观察数据:树套树 PASS    主席树 PASS  一层一个Trie PASS 再看,异或!我们就把目光暂时定在01Tire然后我们发现,我们可以带着一堆点在01Trie上行走,因为O(n*q* ...

  4. poj2814-拨钟问题-C语言-枚举算法

    #include <stdio.h> #include <stdlib.h> /* 首先,我们考虑用长度为9的数组表示表盘的状态以及调表的操作,终止的条件是表盘状态数组所有元素 ...

  5. [链接] Linux下常见的~/.bashrc、/etc/profile、/etc/ld.so.config小科普以及caffe编译遇到的相关问题解决

    由于博主设置禁止转载,这里贴一个链接,http://blog.csdn.net/u014266895/article/details/61928602,内容很有用,linux下很多软件问题都是各种路径 ...

  6. js 内置对象属性及方法

    1.Date 属性(1): constructor      所建立对象的函数参考 prototype       能够为对象加入的属性和方法 方法(43): getDay()        返回一周 ...

  7. 解读dbcp自动重连那些事

    转载自:http://agapple.iteye.com/blog/791943 可以后另一篇做对比:http://agapple.iteye.com/blog/772507 同样的内容,不同的描述方 ...

  8. 计算1-1/x+1/x*x

    // algo1-1.cpp 计算1-1/x+1/x*x. #include<stdio.h> #include<sys/timeb.h> void main() { time ...

  9. PhoneGap之自定义插件

    PhoneGap:作为原生App,Java(这里面是指Android的)与JavaScript 的通信桥梁,使得我们的混合开发更加得心应手,我是与Android结合的混合开发. 但在这里不得不吐槽一下 ...

  10. 访问localhost与127.0.0.1的区别

    很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什么地址?其实127.0.0.1是一个回送地址,指本地机,一般用来测试使用.大家常用来ping 127.0.0.1来看本地i ...