699 - The Falling Leaves
来源:互联网 发布:淘宝森女部落 编辑:程序博客网 时间:2024/05/16 09:05
The Falling Leaves
PS:因为该题排版较麻烦,这里给出OJ网址:UVa699 - The Falling Leaves
给一棵二叉树,每个结点都有一个水平位置:左子结点在它左边1个单位,右子结点在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。如图6-7所示,从左到右的3个位置的权和分别为7,11,3。按照递归(先序)方式输入,用-1表示空树。
样例输入:5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1 -1 3 7 -1 -1 -1
-1样例输出:
Case 1:
7 11 3
Case 2:
9 7 21 15
#include <iostream>#include <cstring>using namespace std;const int maxNum = 1005;// 每堆叶子的数组int sum[maxNum];// 递归构建// 输入并统计子树,水平位置为posvoid build(int pos) { // 值 int v; cin >> v; // 空树结点 if(v == -1) { return; } sum[pos] += v; build(pos - 1); build(pos + 1);}// 输入并初始化树根,再构建子树bool init() { // 值 int v; cin >> v; if(v == -1) { return false; } memset(sum, 0, sizeof(sum)); // 树根的水平位置 int pos = maxNum / 2; sum[pos] = v; // 构建左子树堆 build(pos - 1); // 构建右子树堆 build(pos + 1);}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++]; } cout << "\n\n"; }}
1 0
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- uva 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- 699 - The Falling Leaves---递归
- UVA 699 - The Falling Leaves
- 699_uva The Falling Leaves
- UVa 699 - The Falling Leaves
- uva 699 The Falling Leaves
- Activity 与Fragments 使用总结
- SDUT 1269 走迷宫
- Android 获得手机信息工具类
- matlab调用opencv的函数
- 配置微信公众号二次开发接口java代码
- 699 - The Falling Leaves
- SQlite数据库使用总结
- 百炼 1753:Flip Game
- MeanJS配置经验
- 手动创建活动(activity)--1
- CentOS搭建NTP服务器
- SDUT 1304 取数字
- git 常用命令
- 关闭error, forbidden warning