UVA699-落叶-二叉树
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-落叶-二叉树的更多相关文章
- 6_10 下落的树叶(UVa699)<二叉树的DFS>
每年到了秋天树叶渐渐染上鲜艳的颜色,接着就会落到树下来.假如落叶发生在二叉树,那会形成多大的树叶堆呢?我们假设二叉树中的每个节点所落下的叶子的数目等于该节点所储存的值.我们也假设叶子都是垂直落到地面上 ...
- UVa699 The Falling Leaves
// UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉树的递归实现(java)
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
- c 二叉树的使用
简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- Java 二叉树遍历右视图-LeetCode199
题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...
- 数据结构:二叉树 基于list实现(python版)
基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [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, ...
随机推荐
- hdu 5182 结构体排序
BC # 32 : 打 BC 的时候没看全三个关键字,WA 了五发,花了近一小时,问了一下才发现少看一个条件,于是顺利给跪. 题意:给出若干城市的两次空气质量,首先按空气质量差排序,若相等则按第二次排 ...
- 1、ECMAScript 6 简介
ECMAScript 和 JavaScript 的关系 ES6 与 ECMAScript 2015 的关系 语法提案的批准流程 ECMAScript 的历史 部署进度 Babel 转码器 Traceu ...
- GStreamer插件分类
gst-plugins-base一套小而固定的插件,涵盖各种可能类型的elements; 这些在开发系列期间随着核心变化而不断更新.我们相信分销商可以安全地发行这些插件.人们编写插件应该将他们的代码基 ...
- 00.嵌入式Linux开发环境搭建
3.虚拟机上网配置 虚拟机如果要从网上获取资源,就要能够访问外网.虚拟机有三种上网方式:桥接上网,NAT上网,单主机模式[没用过].本节从原理和操作2个方面讲了NAT方式和桥接方式这2种不同的虚拟机 ...
- git add -A、git add -u、git add .区别
git add各命令及缩写 git add各命令 缩写 git add --all git add -A git add --update git add -u git add . Git Versi ...
- Mfs+drbd+keepalived实现mfs系统高可用
http://blog.sina.com.cn/s/blog_53c654720102wo1k.html Moosefs分布式文件系统是一个易用的系统,但其只有在Pro版中提供了master的高可用方 ...
- golang 多个routine之间的同步
本文以一个例子的方式介绍channel在同步中的使用. 下面的例子中,主task首先阻塞,直到两个task完成后,再继续执行. package main import ( "log" ...
- apache-cxf-2.6.3 spring集成配置
apache-cxf-2.6.3 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&qu ...
- Lucene.Net 入门级实例 浅显易懂。。。
Lucene.Net 阅读目录 开始 Lucene简介 效果图 Demo文件说明 简单使用 重点类的说明 存在问题 调整后 Lucene.Net博文与资源下载 做过站内搜索的朋友应该对Lucene.N ...
- js和jquery判断该元素中是否有指定class
<div class="test">test</div> var t = document.getElementsByClassName('test'); ...