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 ...
随机推荐
- matlab中,计算,记录,程序运行,起始,结束 时间,间隔 &matlab中 tic,toc函数的用法
Tic和toc函数可以计算运行一段时间的代码. 例如: clc tic d=zeros(1,10000); for i=1:10000 d(i)=i; end toc tic c=1; for i=1 ...
- Python中文乱码的处理
为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...
- delphi 如何关闭 Unsafe typecast of 和 Unsafe type 的waring
有时在Delphi使用指针类型的数据,总是提示如下: [Warning] FGroupFeedBack.pas(796): Unsafe typecast of 'Pointer' to 'TObje ...
- 用命令行(CMD)中启动和关闭ORACLE服务
lsnrctl start开启监听 lsnrctl stop停止监听 net start oracleserviceoracle开启oracle服务 net stop oracleserviceora ...
- DevExpress GridView属性设置 z
本文主要总结控件的属性设置,附上图片,给大家一个参考.后续会给大家分享功能实现和使用的小技巧. GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据 ...
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- lightoj 1023
题意:让你输出前N个大写字母的前K个排列,按字典序,很水,直接dfs. #include<cstdio> #include<string> #include<cstrin ...
- 软件测试技术(六)——白盒测试之控制流覆盖准则+Visual Studio 2013单元测试
一.目标程序 单片机发送的A/D转换结果的整体格式为:[DLE][STX]Message[CHKSUM][DLE][ETX],其中[]括号中的字符为16进制的助记符,并非ASCII码.其中:[DLE] ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- Machine Learning & Data Mining 资料整合
机器学习常见算法分类汇总 | 码农网 数据挖掘十大经典算法 | CSDN博客 (内含十个算法具体介绍) 支持向量机通俗导论(理解 SVM 的三层境界)| CSDN博客 (强烈推荐关注博主) 教你如何迅 ...