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. CodeForces - 140E:New Year Garland (组合数&&DP)

    As Gerald, Alexander, Sergey and Gennady are already busy with the usual New Year chores, Edward has ...

  2. Struts2访问ServletAPI的三种方式

    web应用中需要访问的ServletAPI,通常只有HttpServletRequest,HttpSession,ServletContext三个,这三个接口分别代表jsp内置对象中的request, ...

  3. centos安装redis +RedisDesktopManager连接redis

    1.先到Redis官网(redis.io)下载redis安装包 wget http://download.redis.io/releases/redis-5.0.4.tar.gztar xzf red ...

  4. hdu2064

    hdu2064 汉诺塔变形,数学题 #include<stdio.h> ]; int main(){ A[]=; int i; ;i<=;i++){ A[i]=*A[i-]+; } ...

  5. ehci及其伴随ohci主机控制器驱动分析

    1. 正常插入 插上U盘产生中断调用usb_hcd_irq: usb_hcd_irq ehci_irq usb_hcd_resume_root_hub queue_work(pm_wq, &h ...

  6. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  7. 批处理(bat)命令学习的一些总结

    这篇笔记是我对批处理学习的一些总结,能在系统帮助里找到的内容我就不写了,太偏门的也不写,只写些个人感觉很好用的技巧,大部分属于整理 一.set 篇: 1.set(无开关) set .=test set ...

  8. javascript 变量定义

    一.javascript中,变量定义的位置与写在哪个<script></script>标签对内无关,只分前后顺序,写在前面的后面就能够访问,写在后面的前面会提示“未定义”. 例 ...

  9. 【python】split 和 join函数

    一.关于split 和 join 方法 1只针对字符串进行处理.split:拆分字符串.join连接字符串2.string.join(sep): 以string作为分割符,将sep中所有的元素(字符串 ...

  10. python中的with

    看例 """ 需求:不用数据库连接池,实现数据库链接操作 """ class SQLHelper(object): def open(sel ...