UVA 699 The Falling Leaves (递归先序建立二叉树)
题目链接: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 (递归先序建立二叉树)的更多相关文章
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
- uva 699 the falling leaves——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3QAAAMsCAIAAACTL3d2AAAgAElEQVR4nOx9y7GuPA4tKRADk/92T8 ...
- UVa 699 The Falling Leaves
题意:给出按先序输入的一颗二叉树,分别求出从左到右的相同横坐标上的节点的权值之和 递归建树,然后用sum数组分别统计每一个横坐标上的权值之和 感觉建树都在递归递归递归= =慢慢理解吧 #include ...
- uva 699 The Falling Leaves dfs实现
额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...
- uva 699 The Falling Leaves(建二叉树同一时候求和)
本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开 ...
- java创建二叉树并实现非递归中序遍历二叉树
java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...
随机推荐
- [Leetcode] Roman to integer 罗马数字转成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Angular Cookie 读写
var app = angular.module('Mywind',['ui.router']) app.controller('Myautumn',function($scope,$http,$fi ...
- Codeforces Round #526 (Div. 2) C. The Fair Nut and String
C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...
- Educational Codeforces Round 54 (Rated for Div. 2) ABCD
A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...
- Substrings Sort string 基本操作
You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...
- LA2995 Image is everything
蓝书P12 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm ...
- node读取文件夹名
const fs = require('fs'); const join = require('path').join; /** * * @param startPath 起始目录文件夹路径 * @r ...
- IOS 学习资料整理{非常有用,强烈推荐}
绝地地的资源博客:我是雷锋不用谢~~啦啦啦 https://blog.csdn.net/kunga0814/article/details/82117090
- iOS 后台运行执行代码(例如定位)
- 【BZOJ】ARC083 E - Bichrome Tree
[算法]树型DP [题意]给定含n个点的树的形态,和n个数字Xv,要求给每个点赋予黑色或白色和权值,满足对于每个点v,子树v中和v同色的点的权值和等于Xv.n<=10^5 [题解]首先每个点的权 ...