1:用一根数轴,根结点是坐标index,左结点-1,右结点+1

还有一个0ms的不知道怎么过的

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<sstream>
using namespace std;
void caculate(int* tree, int index, int* min, int*max)
{
int s;
cin >> s;
if (s != -1)
{
tree[index - 1] += s;
if ((index - 1) < *min)
{
*min = index-1;
}
caculate(tree, index - 1, min, max);
}
cin >> s;
if (s != -1)
{
tree[index + 1] += s;
if ((index +1) > *max)
{
*max = index+1;
}
caculate(tree, index + 1, min, max);
}
}
int main()
{
freopen("d:\\1.txt", "r", stdin);
int MAXN = 10000;
int number = 1;
while (cin)
{
int MIDDLE = 5000;
int total[MAXN];
memset(total, 0, sizeof(int) * MAXN);
int s;
cin >> s;
if (s == -1)
return 0;
else
{
int max = MIDDLE;
int min = MIDDLE;
total[MIDDLE] += s;
caculate(total, MIDDLE, &min, &max);
cout << "Case " << number << ":" << endl;
for (int i = min; i <= max; i++)
if (i == min)
cout << total[i];
else
cout << " " << total[i];
cout << endl<<endl;
}
number++;
}
}

  

UVA699-落叶-二叉树的更多相关文章

  1. 6_10 下落的树叶(UVa699)<二叉树的DFS>

    每年到了秋天树叶渐渐染上鲜艳的颜色,接着就会落到树下来.假如落叶发生在二叉树,那会形成多大的树叶堆呢?我们假设二叉树中的每个节点所落下的叶子的数目等于该节点所储存的值.我们也假设叶子都是垂直落到地面上 ...

  2. UVa699 The Falling Leaves

      // UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  5. c 二叉树的使用

    简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  6. Java 二叉树遍历右视图-LeetCode199

    题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...

  7. 数据结构:二叉树 基于list实现(python版)

    基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...

  8. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  10. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

随机推荐

  1. poj-1015(状态转移的方向(01背包)和结果的输出)

    #include <iostream> #include <algorithm> #include <cstring> #include <vector> ...

  2. HihoCoder - 1051:补提交卡

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho给自己定了一个宏伟的目标:连续100天每天坚持在hihoCoder上提交一个程序.100天过去了,小Ho查看自己的提交 ...

  3. 【vue】Mac上安装Node和NPM

    http://bubkoo.com/2017/01/08/quick-tip-multiple-versions-node-nvm/ 作为前端开发者,node和npm安装必不可少.然而有时会因为安装新 ...

  4. 不用webservice wcf提供服务,用Rest更轻量

    从2005年开始就开始有基于服务的开发方式,到08年时候 微软和sun等公司都已经提供了很多基于服务的开发框架 . 微软 .net 平台的基于服务的框架主要有:.NET Remoting.webser ...

  5. java新的语法糖:Java 8 Lambda表达式

    ***************************************************************************

  6. day31 python学习 并发编程之多进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): 二 进程与程序的区别 程序仅仅只是一堆代码而已,而进程指的是程序的运行 ...

  7. .net 拆分字符串成数数组 包含使用空格 逗号 回车 换行符等

    简单的代码如下: public static string[] GetProductList(string inputstring)        {            char[] split ...

  8. O(n)线性空间的迷宫生成算法

    之前所有的迷宫生成算法,空间都是O(mn),时间同样是O(mn),时间上已经不可能更优化, 于是,我就从空间优化上着手,研究一个仅用O(n)空间的生成算法. 我初步的想法是,每次生成一行,生成后立即输 ...

  9. hadoop大数据处理平台与案例

    大数据可以说是从搜索引擎诞生之处就有了,我们熟悉的搜索引擎,如百度搜索引擎.360搜索引擎等可以说是大数据技处理技术的最早的也是比较基础的一种应用.大概在2015年大数据都还不是非常火爆,2015年可 ...

  10. C语言sscanf用法解析与正则表达式支持

    最近学习算法和输入输出用到的基本知识,首先是我自己写的一份代码参考和学习了很多资源 后面会给出参考资料,他们写得更加详细,正则表达式的支持确实是一大亮点所在 #include<iostream& ...