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, ...
随机推荐
- java8 array、list操作 汇【2】)- (Function,Consumer,Predicate,Supplier)应用
static class UserT { String name; public UserT(String zm) { this.name=zm; } public String getName() ...
- QQ在线客服的使用
<a target="_blank" href="http://wpa.qq.com/msgrd?v=1&uin=2804010556&site=a ...
- Hadoop学习笔记(1)(转)
Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...
- Android USB gadget框架学习笔记
一 Gadget框架结构 kernel/drivers/usb/gadget,这个目录是android下usbgadget的主要目录. Gadget功能组织单元:主要文件android.c,usb g ...
- mysql增删查改和alter
盗用两篇文章吧,因为觉得别人总结的已经够好了 http://blog.csdn.net/evankaka/article/details/45580845 http://www.blogjava.ne ...
- Ribbon Control
https://documentation.devexpress.com/#WindowsForms/CustomDocument2492 The Ribbon Control replaces tr ...
- 7zip命令行中文说明
7z.exe 是 7-Zip 的命令行版本.7z.exe 使用 7-Zip 的其它模块,7za.exe 是7-Zip 的独立版本,7za.exe 仅支持 7z.zip.gzip.bzip2 和 tar ...
- 【python】单下划线与双下划线的区别
Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx 不能用'from moduleimport *'导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 以单下划线开头(_f ...
- 转Python 和C#的交互
http://www.cnblogs.com/wilber2013/category/708919.html IronPython和C#交互 IronPython是一个.NET平台上的Python ...
- Densenet 相关
https://github.com/flyyufelix/DenseNet-Keras