UVa699 The Falling Leaves
// 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的更多相关文章
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- 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 ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
随机推荐
- Java [leetcode 5] Longest Palindromic Substring
问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Oracle 闪回特性(FLASHBACK DATABASE)
--===================================== -- Oracle 闪回特性(FLASHBACK DATABASE) --======================= ...
- 【JS】<a>标签调用js中函数的几种方法
我们常用的在a标签中有点击事件: a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题,而 ...
- redis发布/订阅模式
其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ...
- C++类与对象
[1]类的内存问题 类是抽象的,不占用内存,而对象是具体的,占用 存储空间.在一开始时弄清对象和类的关系是十分 重要的.[2]类的声明 如果在类的定义中既不指定private也不指定public,则系 ...
- JavaScript中的重载解读
在JavaScript中有一种特殊的数据类型---Function类型,JavaScript的每个函数都是Function类型的实例.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针,不会与 ...
- MFC工程目录
如果已经以Debug方式编译链接过程序,则会在解决方案文件夹下和工程子文件夹下各有一个名为“Debug”的文件夹,而如果是Release方式编译则会有名为“Release”的文件夹.这两种编译方式将产 ...
- jsViews validates(验证)
概述:jsViews使得前端开发过程将js代码与html分离,通过模板实现数据与html元素关联,通过绑定方法用数据填充模板,达到渲染html要素的目的.采用该方法后js文件中再也不会出现 ...
- jar,war,ear区别及java基础杂七八
jar,war,earqu区别 这三种文件都可以看作是java的压缩格式,其实质是实现了不同的封装: jar--封装类war--封装web站点ear--封装ejb.它们的关系具体为:jar: ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...