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 ...
随机推荐
- 多线程程序设计学习(4)guarded suspension模式
Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者 1.1该 ...
- 向Oracle中插入记录时,出现“Oracle.DataAccess.Client.OracleException ORA-00933 ”错误
错误信息的弹出框
- codeforces 696B Puzzles 树形概率计算
题意:给一棵有根树,从根节点深搜,每次随机走,问每个点的dfs序的期望是多少 分析:对于每一个点,它的所有祖先节点dfs序肯定在它之前,它所在的子树的节点一定在它后面, 剩下的是既不是子树又不是祖先的 ...
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- <转>浅谈DNS体系结构:DNS系列之一
浅谈DNS体系结构 DNS是目前互联网上最不可或缺的服务器之一,每天我们在互联网上冲浪都需要DNS的帮助.DNS服务器能够为我们解析域名,定位电子邮件服务器,找到域中的域控制器……面对这么一个重要的服 ...
- 静态文件服务器(The static file servers)
大部分的网站都会提供一些在通常操作下不会发生改变的资源给浏览器.显示网站外观的图片和CSS文件,在浏览器中运行的JavaScript代码,没有动态组件的HTML文件就是这种资源中的代表,统称为静态文件 ...
- 软件开发杂谈之从需求到上线---valen
背景 IT已经成为当代企业必不可少的竞争手段,从无到有到标配,可以说以后不懂IT的就是文盲这句一点也不过,而软件开发是个复杂工程,零零碎碎各种理论工具和技巧,一言难尽. 本文意在言简意赅,简述软件开发 ...
- NodeJS学习:爬虫小探补完计划
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 书接上回,我们需要修改程序以达到连续抓取40个页面的内容.也就是说我们需要输出每篇文章的标题.链接.第一条评论.评论用户和论坛 ...
- redis的sets类型
set是集合 , 它是string类型的无序集合 . set是通过hash table 实现的 , 添加.删除和查找的复杂度都是O(1) . 对集合我们可以取并集.交集.差集.通过这些操作我们可以实现 ...
- cos
Apple过于封闭,没啥朋友,这家伙应该比较高傲,曾仅和Intel,IBM and so on..一起玩过!Google过于开放,狐朋狗友,友人泛滥,殃及ecosystem,弊端已显,祸水将至.COS ...