The Falling Leaves UVA - 699
题目链接:https://vjudge.net/problem/UVA-699


题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。
思路:用sum[i]表示第i个水平位置的总和 。 其实这题并不难 但是因为刚刚学数据结构 二叉树并不熟悉 所以也列出来
看代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn=1e6+;
int sum[maxn];
void build(int p)
{
int v;
cin>>v;
if(v==-) return ;
sum[p]+=v;
build(p-);
build(p+);
}
bool init()
{
int v;
cin>>v;
if(v==-) return false;
memset(sum,,sizeof(sum));
int pos=maxn/;
sum[pos]=v;
build(pos-);
build(pos+);
return true ;
}
int main()
{
int ca=;
while(init())
{
int p=;
while(sum[p]==) p++;//找到最左边的叶子
//cout<<"Case "<<++ca<<":"<<endl;
printf("Case %d:\n",++ca);
cout<<sum[p++];
while(sum[p]!=) cout<<" "<<sum[p++];
cout<<endl<<endl;
}
return ;
}
The Falling Leaves UVA - 699的更多相关文章
- 二叉树的递归遍历 The Falling Leaves UVa 699
题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...
- 【紫书】 The Falling Leaves UVA - 699 递归得简单
题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新le ...
- 下落的树叶 (The Falling Leaves UVA - 699)
题目描述: 原题:https://vjudge.net/problem/UVA-699 题目思路: 1.依旧二叉树的DFS 2.建树过程中开个数组统计 //紫书源代码WA AC代码: #include ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
随机推荐
- 优先队列详解priority_queue .RP
) 删除.在最小优先队列(min priorityq u e u e)中,查找操作用来搜索优先权最小的元素,删除操作用来删除该元素;对于最大优先队列(max priority queue),查找操作用 ...
- 《Maven实战》笔记-3-Maven仓库
一.Maven仓库的分类 1.本地仓库 一般来说,在Maven项目目录下,没有诸如lib/这样用来存放依赖文件的目录. 要自定义本地仓库目录地址时,可以编辑文件~/.m2/setting.xml,设置 ...
- Required plugin could not be found. Videos requires to install plugins to play files of the following types : MPEG-4-AAC decoder and H.264 decoder
https://linuxconfig.org/how-to-install-mpeg-4-aac-decoder-for-centos-7-linux 记得联网情况下才能下载.
- C# winForm 开机自动启动 并启动后最小化到任务栏 右侧通知栏并交互操作
//设置自动启动 string path = Application.StartupPath; SettingHel.SetAutoRun(path +@"\MostImpressive.D ...
- ObjectARX环境搭建之vs2010+objectArx2012+AutoCAD2012
---------------------------------------------------------------------------------------------------- ...
- 2015年第六届蓝桥杯省赛试题(JavaA组)
1.结果填空 (满分3分)2.结果填空 (满分5分)3.结果填空 (满分9分)4.代码填空 (满分11分)5.代码填空 (满分13分)6.结果填空 (满分17分)7.结果填空 (满分21分)8.程序设 ...
- Git相关安装包打包下载
Git相关软件偶尔需要***才能下载,故分享于此 1.Git-2.15.0-64-bit.exe 2.TortoiseGit-2.5.0.0-64bit.msi 3.TortoiseGit-Langu ...
- [raspberry pi3] 串口线使用
直接上图 GPIO 口说明: 串口线连接: 参考地址: http://bbs.elecfans.com/jishu_596578_1_1.html
- VMWare虚拟机无法打开内核设备"\\.\Global\vmx86"的解决方法
cmd执行: 1.net start vmci 2.net start vmx86 3.net start VMnetuserif
- winform改变控件的外形
GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 40, 40); Region region = new Region(gp); c ...