【数据结构】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(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
随机推荐
- 洛谷 P3375 【模板】KMP字符串匹配
我这段时间因为字符串太差而被关了起来了(昨晚打cf不会处理字符串现场找大佬模板瞎搞,差点就凉了),所以决定好好补一下字符串的知识QAQ,暂时先学习kmp算法吧~ 题目链接:https://www.lu ...
- ribbon使用eureka的meta进行动态路由
序 使用eureka的元数据信息,再配上ribbon的路由功能,就可以在api-gateway实现很多功能,比如灰度测试.生产调试等等.下面介绍一下,怎么使用jmnarloch大神提供的ribbon- ...
- Pythone3 sys模块
1.sys.argv 可以实现从程序外部向程序传递参数2.sys.exit() 程序中间退出,exit(0)正常退出,其他为异常退出3.sys.getdefaultencoding() 获取系统编码方 ...
- dev_cpu_dead
Kernel: 4.12.6 每个cpu都有自己的softnet_data结构,用来处理数据包接收,但是当softnet_data所在cpu无法工作时,即CPUHP_NET_DEV_DEAD状态,就需 ...
- MACHINE_START与MACHINE_END【转】
转自:http://blog.csdn.net/cxw3506/article/details/8475965 版权声明:本文为博主原创文章,未经博主允许不得转载. 在移植Linux时,有个结构体需要 ...
- 【设计模式】原型模式(Prototype)
摘要: 1.本文将详细介绍原型模式的原理和实际代码中特别是Android系统代码中的应用. 纲要: 1. 引入原型模式 2. 原型模式的概念及优缺点介绍 3. 原型模式对拷贝的使用 4. 原型模式在A ...
- 【bzoj1923】外星千足虫
这个gauss消元有点naive啊. 由于只有01,位操作显然是方便的多. 那么用bitset代替之前的增广矩阵就行了. #include<bits/stdc++.h> #define N ...
- SSD算法及Caffe代码详解(最详细版本)
SSD(single shot multibox detector)算法及Caffe代码详解 https://blog.csdn.net/u014380165/article/details/7282 ...
- leetcode 之Rotate Image(8)
这题需要搞清楚矩阵元素的位置关系,尤其是副对角线元素,沿着副对角线元素 void rotateImage(vector<vector<int>> &matrix) { ...
- close()和shutdown()函数
一·close(int sockfd) 当server和client建立连接,server调用close(),则server发送fin给client,server不在通过该套接字继续传送消息或者接收消 ...