UVa 699 The Falling Leaves (DFS遍历二叉树)

【代码超详解】UVA 699 The Falling Leaves 下落的树叶 一、题目描述 有些地方原题说得不太清楚,我按照自己的语言说一遍。 输入是若干棵二叉树,先序遍历,权值代表从该点竖直落下的叶片数量,没有风将任何节点落下的叶片吹到其它地方去。每个节点的左子树比该节点的水平坐标减少一个单位,右子树比该节点的水平坐标增加一个单位,两个子树的垂直坐标比父节点减少一个单位。要求统计处在每个水平位置全部节点的权值之和(作为该处落下的叶片的总数)。如果一个节点的左子树或右子树不... 阅读详情

699 - The Falling Leaves

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=640

Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the "leaves" drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input 

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree).

Output 

For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.

Sample Input 

5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1

Sample Output 

Case 1:
7 11 3

Case 2:
9 7 21 15

水题。


完整代码:

/*0.046s*/

#include<cstdio>
#include<cstring>

int pile[200], temp;
char str[1000];

void dfs(int pos)
{
	scanf("%d", &temp);
	if (temp < 0) return;
	pile[pos] += temp;
	dfs(pos - 1);
	dfs(pos + 1);
}

int main()
{
	int cas = 0, i;
	while (scanf("%d", &pile[100]), pile[100] > 0)
	{
		dfs(99);
		dfs(101);
		printf("Case %d:\n", ++cas);
		for (i = 0; !pile[i]; ++i)
			;
		for (; pile[i + 1]; ++i)
			printf("%d ", pile[i]);
		printf("%d\n\n", pile[i]);
		memset(pile, 0, sizeof(pile));
	}
	return 0;
}

深入解析UEFI PEI阶段:PeiCore的三次调用与内存管理机制 本文深入解析UEFI启动流程中的PEI阶段,重点探讨PeiCore的三次调用与内存管理机制。详细介绍了PeiCore在临时内存中的初始化、堆栈切换至永久内存的过程,以及最终交接给DXE阶段的关键步骤,为开发者提供调试技巧与实战案例,助力解决UEFI启动问题。 阅读详情

相关推荐

Python数据分析案例——宏观经济指标与沪深指数实证研究分析

沪深300指数与宏观经济指标关联性研究:2015-2024实证分析 摘要:本文基于2015-2024年月度数据,采用多元回归方法分析宏观经济指标对沪深300指数的影响。研究发现,市场情绪(SEN)、美国利率(STY)、美元指数(DXY)、狭义货币供应量(M1)和CPI对指数具有显著解释力(P<0.05),而GDP、LPR等传统指标影响不显著。模型解释力达62%(R²=0.62),表明A股短期走势更受资金流动、投资者心理和国际金融条件驱动。研究为投资者提供了除基本面外的市场预判指标,建议关注M1增速、美

weixin_50902344的博客 1412

UVA 699 下落的树叶;

思路:递归;递归建立二叉树,然后输出就可以; 首先在输入上,这个输入的判断就是如果等于 -1,就返回;一直在递归输入,首先输入root的值;然后建树,递归输入,先序,就先build(root-1),再build(root+1);就

altoer的博客 304

S32DS使用完整指南:S32K CAN通信模块配置步骤

深入讲解S32DS使用过程中S32K芯片的CAN通信模块配置步骤,涵盖关键设置与常见问题。掌握s32ds使用技巧,提升开发效率,实现稳定可靠的车载网络通信。

weixin_42592399的博客 991

UVA699 The Falling Leaves 解题报告

先序递归隐式建树,同时统计每一列的和。我们并不需要真的建树,在递归的过程中我们无需知道每个节点的具体编号,只需要知道当前节点列编号即可,为了方便实现,我们将整棵树的根节点视为0列,往左为-1列,往右为+1列,这样做列数会有负数,所以使用map来维护每一列的和。给一棵二叉树,每个结点都有一个水平位置:左子结点在它左边1个单位,右子结点在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。如图6-7所示,从左到右的3个位置的权和分别为7,11,3。按照递归(先序)方式输入,用-1表示空树。

qq_45554473的博客 449

手把手教你用Cadence Virtuoso设计折叠式共源共栅放大器(0.18μm工艺实战)

本文详细介绍了在0.18μm工艺下使用Cadence Virtuoso设计折叠式共源共栅放大器的完整流程。从工艺参数获取、电路架构分析到具体参数计算和仿真验证,提供了实战设计指南,帮助工程师掌握这一模拟IC设计中的关键技术,特别适合需要中等增益和较大输出摆幅的应用场景。

open4的博客 417

游戏开发实战:5种AOI算法对比(九宫格 vs 灯塔 vs 十字链表)

本文深入对比了游戏开发中五种主流AOI算法(九宫格、灯塔、十字链表等)的性能与适用场景,帮助开发者根据项目规模选择最优解决方案。通过详细的技术分析和实战数据,揭示不同算法在万人同屏等极端场景下的表现差异,为MMORPG等大型多人在线游戏提供关键性能优化指南。

weixin_29268385的博客 484

UVA - 699 The Falling Leaves

给一棵二叉树,每个结点都有一个水平位置:左子结点在它左边1个单位,右子结点在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。如图6-7所示,从左到右的3个位置的权和分别为7,11,3。按照递归(先序) 方式输入,用-1表示空树。 样例输入: 5 7 -1 6 -1 -1 3 -1 -1 8 2 9 -1 -1 6 5 -1 -1 12 -1 -1 3 7 -1 -1 -1 -1...

衍射的博客 210

REST-meta-MDD Data总结

sMRI数据使用:nwc1、nwc2、nwc3(同样数据集相关论坛中有相关信息指向需要使用wc1、wc2、wc3)多方咨询与讨论后决定使用nwc数据,这里说明wc数据相对wc数据要大的多如wc1的大小为1.33GB,而nwc1的数据却有6.90GB。VBM文件:/REST-meta-MDD/Sharing/REST-meta-MDD-VBM- phase1 -Sharing.tar.gz。1、Excel中的表型信息:1-男,2-女;-9999或空白表示缺少数据。

2404_88295660的博客 1557

【UVA699】下落的树叶 The Falling Leaves

树的练习

2301_76605150的博客 475

补洞与fairing算法

对网格进行补洞,remesh, 平滑,fairing, 最小二乘优化平滑

闪电彬彬的博客 1055

【白话系列】倍增算法

【序言】         我认为吧,所有能够优化复杂度的算法都是神奇的,所有能够化繁琐为形象的文字都是伟大的。一直觉得倍增算法是个很神奇的东西,所以决定写点东西纪念一下它。但是作为一个非常不称职的OIER,我非常讨厌在看别人的算法解析时整版的i,j,k等我看到鼠标就惯性移到右上角的符号语言,所以我想用最形象的方式来纪念它。 【一】         从前,有一只可爱得不得了的小白兔,它想从

JarjingX 3万+

UVA699-例题6-10 下落的树叶(The Falling Leaves,UVa 699)

原题链接: UVa-699 题目大意:   题意为给一棵二叉树,求从左向右输出每个水平位置的所有结点的权值之和。输入按照递归(先序)方式,-1表示空树。 解题思路:  本题是《算法竞赛入门经典》第六章例题,本题的主要解题思路就是使用递归根据输入直接计算每个水平位置的的权值之和。  一开始看完题之后也想着用数组来存储每一列的权值之和。但是由于下标的问题感觉处理起来过于麻烦。就有

Miracle42的博客 1505

巨正则蒙特卡洛—甲烷和二氧化碳的竞争吸附GCMC

巨正则蒙特卡洛—甲烷和二氧化碳的竞争吸附GCMC

Rockyoung 8036

uva 699 The Falling Leaves

题目大意:如下图的二叉树,求出同一列中的数的和,下图的输出为:7 11 3 #include<iostream> #include<algorithm> #include<queue> #include<cmath> #include<math.h> #include<string> #include<stri...

qq_35752161的博客 217

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

例题6-10 UVA699 The Falling Leaves(23行AC代码)

紫书刷题进行中,题解系列【GitHub|CSDN】 例题6-10 UVA699 The Falling Leaves(23行AC代码) 题目大意 以树根节点为中心,画一根垂直线(可看成y轴),因此树根坐标为0,它的左子树根为-1,右子树根为+1,依次类推,即往左走-1,往右走+1,现在将坐标相同的点看成一叠,从左到右输出他们的权值和。 思路分析 和例题6-9有些类似,直观思路是想通过输入先序建树,...

努力做个书呆儿 359

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

The Falling Leaves UVA - 699 (二叉树列和)

解题思路: 可以设定一个中间值pos,每次插入结点时往左节点为pos-1,右节点为pos+1,开一个数组累加每个pos的和,最后输出 数据的输入为先序遍历顺序,-1为空结点 if (cin.get() == '\n') return; 可以检测回车换行 #include <iostream> #include <algorithm> #include <...

alex1997222的博客 177

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

数据结构-树与二叉树-二叉树的递归遍历(Tree   UVA - 548||Not so Mobile UVA - 839||The Falling Leaves UVA - 699)

目录 二叉树的递归遍历: Tree UVA - 548 题目大意: 基本思路: 输入控制: 代码: Not so Mobile UVA - 839 The Falling Leaves UVA - 699 二叉树的递归遍历: Depth-First Search(DFS):先序遍历 中序遍历 后序遍历 Tree UVA - 548 题目大意: 知道中序遍历和后序遍历的顺序,算出该二叉树并求出最小权值路径对应的叶结点权值(结点值为...

m0_50623076的博客 189

UVa 699 - The Falling Leaves 二叉树的落叶

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=640 题目类型: 数据结构, 二叉树 题目大意: 每年的秋天,   北方的树叶伴随着灿烂无比的颜色,    叶

D_Double's Journey 1928

UVA 699 The Falling Leaves(二叉树|DFS)

题目链接题意给出一棵二叉树的先序遍历。 每一个节点都有一个水平位置,它的左儿子在它左边一个单位的位置,右儿子在它右边一个单位的位置。 从左到右输出每个位置的节点权值和解决 递归建树即可 #include<bits/stdc++.h> using namespace std; const int maxn = 1001;int sum[maxn]; void build(int val , int

ZhangYang 364

UVA 699 - The Falling Leaves(从中间向两边递归perfect)

#include #include #include #include #include #include #include #include using namespace std; #define M 1000+10 typedef struct TNode { int data; TNode *lchild,*rchild; } TNode,*BiTree;

u014682977的专栏 416

下落的树叶(The Falling Leaves, UVa 699)

#include &lt;iostream&gt; #include &lt;cstdio&gt; //6-10 下落的树叶(The Falling Leaves, UVa 699) /* 给一棵二叉树,每个结点都有一个水平位置: 左子结点在它左边1个单位,右子结点在右边1个单位。 从左向右输出每个水平位置的所有结点的权值之和。 如图6-7所示,从左到右的3个位置的权和分别为7,11,3。 按照递...

明觉 355

UVa 699 The Falling Leaves

题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归先序遍历输入,-1表示空树 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int n,date,x,y; int Left,Right; int

沙茶酱的代码库>__< 218

Uva699 The Falling Leaves

Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing hap

UncleJokerly 444

UVA699 下落的树叶 The Falling Leaves

做了上一题,对二叉树的递归加深了理解,所以这次不用建树就可以写出来了,用了map映射,然后输出的时候遍历输出就行了,因为map已经帮你排好序了,需要注意的是map是全局变量,每次用完要清零,让下一次用 #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define mk make_pair #define sz(x) ((int) (x).size()) #define all(x

qq_33942309的博客 163

UVA - 699 D - The Falling Leaves

题意:有一棵二叉树,每个节点都有一个水平位置,左子节点在它左边一个单位,右子节点在右边一个单位,从左向右输出每个水平位置的所有节点之和。 分析:不用建树,可以直接遍历。用一个100的数组,正中间是根的位置,往左就减1,往右就加1,把落叶数加上去,模仿dfs,去遍历一遍,在把数组不是0的输出。 代码: #include&lt;stdio.h&gt; #include&lt;string.h...

Vace___yun的博客 313

uva 699 The Falling Leaves dfs实现

额,刘汝佳小白里面的配套题目。 题目求二叉树同垂直线上结点值的和。 可以用二叉树做,挺水的其实。 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了。 代码: #include <cstdio> #include <cstring> const int maxn = 10000; int n = 500, tmp, num ...

weixin_30323961的博客 68
上一篇: POJ 2106 / México and Central America 2004 Boolean Expressions (用栈处理表达式)
下一篇: UVa 548 Tree (中序遍历&后序遍历&DFS)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值