The Falling Leaves(建树方法)
2015-11-28 16:47
435 查看
uva 699
紫书P159
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?
We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:
View Code
紫书P159
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?
We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:
#include <iostream> #include <cstring> using namespace std; const int maxn=200; int sum[maxn]; void Build(int u) { int v; cin>>v; if(v==-1) return; sum[u]+=v; Build(u-1); Build(u+1); } bool Init() { memset(sum,0,sizeof(sum)); int v; cin>>v; if(v==-1) return false; int pos=maxn/2; sum[pos]=v; Build(pos-1); Build(pos+1); } int ca=1; void Output() { int p=0; while(sum[p]==0) p++; cout<<"Case "<<ca++<<":\n"<<sum[p]; while(sum[++p]!=0) {cout<<" "<<sum[p];} cout<<"\n\n"; } int main() { while(Init()) { Output(); } return 0; }
View Code
相关文章推荐
- nginx 源码学习笔记(二)——nginx精粹-模块
- WebHDFS REST API
- TEXT到EPUB格式转换
- 项目总结:以网络安全为例的大数据可视化设计
- 高精度模板
- 1-4-02:输出绝对值
- Document Filtering(naive bayes method) used by python
- 百度地图API示例之添加定位相关控件
- Safecracker
- nginx 源码学习笔记(一)——初识nginx helloworld模块
- 案例:弹幕表白技术
- 手电筒之指南针
- 博客散乱说
- C API libhdfs
- yacc的运行参数
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- 利用TweenPosition 动画制作直角动画播放。
- 123面试题
- LRU缓存介绍与实现 (Java)
- CentOS 6.4识别NTFS格式的方法