【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves
算法入门经典第6章例题6-10(P159)
题目大意:有一颗二叉树,求水平位置的和。
试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1,向右+1,统计答案即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=1000;
const int INF=999999;
int N,M;
int sum[MAXN];
int Min,Max;
int cnt; void solve(int pos){
int ls=read();
Max=max(Max,pos);
Min=min(Min,pos);
if(ls!=-1){
sum[pos-1+200]+=ls;
solve(pos-1);
}
int rs=read();
if(rs!=-1){
sum[pos+1+200]+=rs;
solve(pos+1);
}
return ;
}
int k;
int main(){
while(scanf("%d",&k)!=EOF){
if(k==-1) break;
memset(sum,0,sizeof(sum));
Min=Max=0;
sum[200]+=k;
solve(0);
printf("Case %d:\n",++cnt);
for(int i=Min;i<Max;i++){
printf("%d ",sum[i+200]);
}
printf("%d\n\n",sum[Max+200]);
}
}
【数据结构】The Falling Leaves(6-10)的更多相关文章
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...
- UVa699 The Falling Leaves
// UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
随机推荐
- Linux中关机,重启,注销命令
关机: shutdown -h now #立刻关机重启,工作中常用 shutdown -h +1 #1分钟后关机 init 0 halt #立即停 ...
- Java 对象排序详解
很难想象有Java开发人员不曾使用过Collection框架.在Collection框架中,主要使用的类是来自List接口中的ArrayList,以及来自Set接口的HashSet.TreeSet,我 ...
- Perl6 Bailador框架(2):路径设置
use v6; use Bailador; =begin pod get表示是get发送 post表示是post发送 get/post 后面的 '/name' 表示是路径 => sub {} 是 ...
- linux中的tasklet机制【转】
转自:http://blog.csdn.net/yasin_lee/article/details/12999099 转自: http://www.kerneltravel.net/?p=143 中断 ...
- 海量数据排序——如果有1TB的数据需要排序,但只有32GB的内存如何排序处理?
转载:https://blog.csdn.net/fx677588/article/details/72471357 1.外排序 传统的排序算法一般指内排序算法,针对的是数据可以一次全部载入内存中的 ...
- exit()与_exit()区别
exit()与_exit()都是用来终止进程的函数,当程序执行到两者函数时,系统将会无条件停止剩下操作,清除进程结构体相应信息,并终止进程运行. 二者的主要区别在于:exit()函数在执行时,系统会检 ...
- linux===linux后台运行和关闭、查看后台任务(转)
fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的 一.& 最经常被用到这个用在一个命令的最后,可以把这个命令放 ...
- x64dbg
https://x64dbg.com/ https://github.com/x64dbg/x64dbg https://sourceforge.net/projects/x64dbg/files/s ...
- tcp窗口机制(写的最简单精炼的文章)
tcp窗口机制(写的最简单精炼的文章) http://blog.csdn.net/occupy8/article/details/48468445
- 【LOJ2254】SNOI2017一个简单的询问
莫队,每次询问的是两个区间,就把区间拆开,分开来算就好了. 借鉴了rank1大佬的玄学排询问的姿势. #include<bits/stdc++.h> #define N 50010 typ ...