UVA - 699 The Falling Leaves

UVA699 下落的树叶 The Falling Leaves(二叉树的递归遍历建树) UVA699 下落的树叶 The Falling Leaves bool型的函数一定不能忘了组后要加上return 1;或者return 0;!!!! #include<bits/stdc++.h> using namespace std; #define debug(x) cout<<"# "<<x<<" "<<endl; type... 阅读详情


  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 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


题意:

每个叶子在垂直方向落叶,最后垂直和是多少?

用个辅助数组sum[MAX],来保存竖直方的和值。

MAX/2作为树叶的根节点,然后通过build(pos-1) 和 build(pos+1) 来模拟建立一颗二叉树

最后输出sum不为0的数


#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <string.h>
using namespace std;
const int MAX = 2000;
int sum[MAX];

void build(int pos) {
	int v;
	cin >> v;

	if(v == -1) //如果是空树则不用考虑
		return ;
	sum[pos] += v;
	build(pos-1);
	build(pos+1);
}

bool init() {
	int v;
	cin >> v;
	if(v == -1){
		return false;
	}
	memset(sum,0,sizeof(sum));
	int pos = MAX/2;
	sum[pos] += v;
	build(pos - 1);
	build(pos + 1);
	return true;
}

int main() {
	int cas = 1;
	while (init()) {
		
		int p = 0;
		while(sum[p] == 0)
			p++;
		cout<<"Case "<<cas++<<":"<<endl;
		cout<<sum[p++];
		while(sum[p] != 0)
			cout<<" "<<sum[p++];
		cout<<endl<<endl;
	}
	return 0;
}


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

相关推荐

js树叶空中飘落特效

jQuery制作树叶空中飘落特效,随机生成树叶,点击树叶就飘落,在空中再点击树叶,也便多片树叶飘落。

POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树。 都是两个基本操作解决: 1 二叉树插入操作 2 前序遍历 简单题目了。 #include #include #include #include using std::vector; using std::string; const int MAX_B = 1024; char buf[MAX_B

靖空间 1326

jQuery不同树叶掉落特效代码

jQuery不同树叶掉落特效代码,点击“添加更多树叶按钮”可以在顶部添加叠加更多的树叶,点击树叶会立刻掉落。

UVa 699 - The Falling Leaves

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 tre

Invalid_Pointer的专栏 753

机器学习发展历史及现状(重要的期刊,会议,大牛总结)

1 引言 机器学习是指通过计算机学习数据中的内在规律性信息,获得新的经验和知识,以提高计算机的智能性,使计算机能够像人那样去决策。随着各行业对数据的需求量增多,对处理和分析数据的效率要求变高,一系列机器学习算法应运而生。机器学习算法主要是指通过数学及统计方法求解最优化问题的步骤和过程。针对不同的数据和不同模型需求,选择和使用适当的的机器学习算法可以更高效地解决一些实际问题。本文将从机器学习的起源...

Oscar2018的博客 5万+

Android dex, odex, oat, vdex, art文件介绍

下图可以简化将Java源转换为OAT的过程: .dex Dalvik虚拟机字节码文件 .vdex 其中包含APK的未压缩DEX代码,以及一些旨在加快验证速度的元数据 .odex .oat // JIT,Just-in-time,即时编译,边运行边编译; // AOT,Ahead Of Time,提前编译,指运行前编译。 其中包含APK中已经过AOT编译的方法代码。 原始DEX文件(例如classes.dex)被转换为另一个包含本机代码的文件。这个新文件通常具有.odex。.oat扩展名,并由ELF格式

he980725866的博客 3619

The Falling Leaves (二叉搜索树+stl)

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 ha...

自渡的博客 428

基于Python的雪球网投资组合数据爬取与分析实战:从入门到精通

在当今数字化投资时代,数据已成为投资者最重要的资产之一。雪球网作为中国领先的投资社交平台,汇聚了大量优质的投资组合数据,这些数据对于量化投资研究和个人投资决策具有重要价值。本文将详细介绍如何使用Python最新技术栈构建一个高效、稳定的雪球网投资组合爬虫,并深入分析获取的数据。投资组合持仓明细及变动历史投资收益率曲线与风险指标投资者操作策略与调仓频率热门行业与个股关注度。

2201_76125261的博客 920

【UVA 699 --- The Falling Leaves】二叉树

【UVA 699 --- The Falling Leaves】二叉树 Description 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 ac...

来日浅谈的博客 447

第4章 部署负载均衡 Keepalived + HAProxy

本章在所有 Master 节点部署 Keepalived + HAProxy,为 kube-apiserver 提供高可用入口。Keepalived 实现 VIP(192.168.3.59)漂移,HAProxy 轮询转发请求至三个 Master 的 6443 端口(监听 6444 避免冲突)。健康检查脚本连续 5 次失败则停止 Keepalived,触发 VIP 自动切换。本章还包含配置详解、日常运维和问题排查。

幸福街--linux技术分享 120

GEE:关于遥感生态指数(RSEI)的若干疑问

本文记录了若干关于在 Google Earth Engine (GEE)平台上,计算基于主成分分析(PCA)的遥感生态指数(RSEI)的问题。并记录了解决这些问题的一些答案。问题包括,主成分分析的特征值的正负,主成分的占比,去云后空值像素的处理方法,异常值处理方法,如何看载荷等。

养乐多的博客 3417

UVa699 The Falling Leaves

// UVa699 The Falling Leaves // Rujia Liu // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空树 // 算法:在“建树”的同时计算,无须真正的把树保存下来 #include #include using namespace std;

zhouzhijian97的博客 304

图注意力网络(GAT)原理与代码实战案例讲解

1. 背景介绍 图注意力网络(Graph Attention Network,简称GAT)是一种基于图神经网络(Graph Neural Networks,简称GNN)的深度学习方法。它能够处理具有复杂关系的节点数据,并在多个领域取得了显著的效果,如社交网络分析、推荐系统等。 2. 核心概念与联系

AI架构师小马 2712

UVa699-The Falling Leaves(下落的叶子)

UVa 699 - The Falling Leaves(下落的叶子) 给一棵二叉树,每个节点都有一个水平位置:左子节点在它左边1个单位,右子节点在右边一个单位。从左向右输出每个水平位置的所有节点的权值之和。按照递归(先序)方式输入,用-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

Never Settle ! 332

【报错解决】OpenClaw 报错 ENOENT: no such file or directory, open ‘/root/.npmrc‘:配置文件路径不存在

本文分析了OpenClaw平台在国产操作系统环境下运行时出现的ENOENT: no such file or directory, open '/root/.npmrc'错误。该问题源于容器环境中root用户的npm配置文件缺失,导致插件依赖安装失败。文章详细解释了npm配置加载机制和容器环境的特殊性,提出了四种解决方案:手动创建配置文件、写入基础配置、使用全局配置替代以及容器启动时自动初始化。最后指出该错误暴露了工程中对默认路径盲目信任的问题,强调显式声明运行环境依赖的重要性。

博客 2015

LeRobot 入门教程(三)在仿真环境和自定义机器人上部署 LeRobot

本教程详细介绍了基于真实世界机器人和仿真环境的模仿学习实现方法,涵盖数据采集、训练策略和评估全流程。在真实机器人部分,重点讲解了远程操作设置、摄像头配置、数据集记录与管理技巧、策略训练(支持GPU/MPS设备)以及模型评估。仿真环境部分则演示了gym-hil的安装使用、模拟数据采集和策略训练。此外,教程还提供了自定义机器人集成指南,包括电机选择、接口继承、传感器/动作特征定义、连接管理和校准配置等核心步骤。所有方法均支持HuggingFace数据集中心和Weights & Biases工具链,为机器

2506_90492529的博客 2399

uva 699 - The Falling Leaves

// uva 699 - The Falling Leaves // 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=640 // 题目大意:给出一棵二叉树先序遍历的结果,-1表示节点为空,每个节点垂直往下落叶,落叶的数量

yzx930420的专栏 360

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

UVA 699 The Falling Leaves

https://vjudge.net/problem/UVA-699 题目 水得不行,就不说了…… 题解 水题,只是注意数组不能开太大……直接就TLE了(缩小10倍用了0.9秒,服了……) AC代码(其实没有看的必要) #include<bits/stdc++.h> using namespace std; #define MAXN 1000007 ...

159

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

uva 699 The Falling Leaves

uva 699 题目链接 题目大意:给一棵二叉树(先序),求每一列的和,从左到右输出 5,6是重合的与1是一列 解题思路:从数轴中间开始左孩子往左移,右孩子往右移,将值加进去 结果就是答案 注意问题:值为-1,则表示为空,                   树为空输入结束                   数据按先序读入            

374

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

uva 699 The Falling Leaves (二叉树)

uva 699 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 und

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 744

下落的树叶 The Falling Leaves UVa 699

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

qq_43749262的博客 422

The Falling Leaves 【UVA - 699 】

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 ha...

Meant_To_Be的博客 224

uva-699 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 ha

超 1705
上一篇: UVA - 839 Not so Mobile
下一篇: POJ - 1539 Evaluating Simple C Expressions(简单的C表达式)
HelloWorld10086
博客等级 码龄12年 88粉丝 614原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值