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, ...
随机推荐
- ajax请求成功回调函数没有执行问题
如下常见的ajax前端请求,请求成功后success:function(result){ 这里的数据没有执行 }: $.ajax({ type:"post", url:" ...
- 利用WebApplicationInitializer配置SpringMVC取代web.xml
对于Spring MVC的DispatcherServlet配置方式,传统的是基于XML方式的,也就是官方说明的XML-based,如下: <servlet> <servlet-na ...
- Weston学习
来源网址:http://blog.csdn.net/fyh2003/article/details/49253713 Weston启动时会读取weston.ini这个配置文件,其中可以配置桌面,动画和 ...
- Patch打补丁学习笔记
1.基本命令语法: patch [-R] {-p(n)} [--dry-run] < patch_file_name p:为path的缩写. n:指将patch文件中的path第n条’/’及其左 ...
- 系列文章--从零开始学习ASP.NET MVC 1.0
从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...
- tomcat源码阅读之Server和Service接口解析
tomcat中的服务器组件接口是Server接口,服务接口是Service,Server接口表示Catalina的整个servlet引擎,囊括了所有的组件,提供了一种优雅的方式来启动/关闭Catali ...
- java编译需要文件后缀名.java 而运行不需要后缀名.class
对于java源文件HelloWorld.java编译命令:javac HelloWorld.java运行命令:java HelloWorld 编译需要文件后缀名.java 而运行不需要后缀名.clas ...
- GNU C与ANSI C的不同
引用:http://tsroad.lofter.com/post/376316_57ac519 1.GNU C可定义0长度数组,目的是为了定义可变长结构体. struct var_struct{ ...
- oracle 恢复备份
select * from dbconninfo update dbconninfo set url = 'jdbc:oracle:thin:@(description=(address_list=( ...
- Maven基本使用
Maven基本使用 一.安装 先去官网下载maven(http://maven.apache.org/download.cgi) 下载下来解压后如下所示 配置环境变量 查看配置成功与否 mav ...