UVa 699The Falling Leaves解题报告
来源:互联网 发布:ruby php 效率 编辑:程序博客网 时间:2024/04/28 05:28
题意:求二叉树垂直方向节点的和。
思路:由于题目给出的数据是前序遍历的,最直接的方法是建树模拟。最简单的方法是用数组模拟求和。我用的是数组模拟,以根节点为中心,遇到左树-1,遇到右树+1,向两边扩展,利用建树的递归思想。
#include <iostream>#include <cstring>using namespace std;int ans[100];void build(int, int);int main(){//freopen("data.txt", "r", stdin);int x;int cases = 1;while (scanf("%d", &x) && x != -1){printf("Case %d:\n", cases++);memset(ans, 0, sizeof(ans));build(50, x);//从中间往两边扩展int flag = 0;for(int i = 0; i < 100; i++){if(ans[i] != 0 && flag == 0){printf("%d", ans[i]);flag = 1;}else if(ans[i] != 0 && flag == 1)printf(" %d", ans[i]);}printf("\n\n");}return 0;}void build(int n, int num){ans[n] += num;int s;scanf("%d", &s);if(s != -1)build(n - 1, s);//读取左孩子scanf("%d", &s);if(s != -1)build(n + 1, s);//读取右孩子return;}
0 0
- UVa 699The Falling Leaves解题报告
- 【Jason's_ACM_解题报告】The Falling Leaves
- uva 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- UVA 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva-699 The Falling Leaves
- UVA 699 The Falling Leaves
- uva 699 The Falling Leaves
- UVA 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- UVA 699 The Falling Leaves
- UVA - 699 The Falling Leaves
- java解析xml文件获取xml里面的信息
- linux 显示控制符号和制表符号
- 插入一个数,进行插入排序
- New Start in 大学
- adb坏了 找不到设备
- UVa 699The Falling Leaves解题报告
- Esper学习之二:事件类型
- 大智慧数据格式
- MAC下安装与配置MySQL
- 获取 设备 版本 型号 是否联网
- 升级故障转移群集(步骤1:升级被动节点)
- 多个div在同一行显示
- Symfony2表单
- mysql 基本语句 (烂笔头)