博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa699 The Falling Leaves
阅读量:7228 次
发布时间:2019-06-29

本文共 1759 字,大约阅读时间需要 5 分钟。

 

// UVa699 The Falling Leaves

// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空

// UVa699 The Falling Leaves// Rujia Liu// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空树// 算法:在“建树”的同时计算,无须真正的把树保存下来#include
#include
using namespace std;const int maxn = 200;int sum[maxn];// 输入并统计一棵子树,树根水平位置为pvoid 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
#include
#include
#include
#include
#include
#include
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<
<
v=i; root->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
q; q.push(root); while(!q.empty()) { Node* nd=q.front(); q.pop(); ans[nd->index - min_index]+=nd->v; //cout<
v<<" "; if(nd->l) q.push(nd->l); if(nd->r) q.push(nd->r); }}void output(){ int i; for(i=0;i

转载地址:http://jzdfm.baihongyu.com/

你可能感兴趣的文章
Lync Server 2010部署与应用(三)---拓扑生成与发布
查看>>
安全摘记1:关于安全与黑客
查看>>
我的友情链接
查看>>
tbox中vector容器的使用
查看>>
一个简单的PHP笔试题
查看>>
firebug重新载入页面获取源码
查看>>
我的友情链接
查看>>
5月末周中国.COM总量净增1.2万个 美国净减2.6万个
查看>>
Elasticsearch数据建模-关联查询
查看>>
我的友情链接
查看>>
CentOS 下安装 Lnmp
查看>>
redis系列:通过日志案例学习string命令
查看>>
世界冠军之路:菜鸟车辆路径规划求解引擎研发历程
查看>>
Linux-sendmail
查看>>
关于BSTR的困惑
查看>>
什么时候使用HashMap?它有什么特点?
查看>>
框架名
查看>>
编译安装PHP
查看>>
插入透明背景Flash的HTML代码
查看>>
无标题
查看>>