下落的树叶 (The Falling Leaves UVA - 699)
题目描述:

题目思路:
1.依旧二叉树的DFS
2.建树过程中开个数组统计
//紫书源代码WA
AC代码:
#include <iostream>
#include <cstring>
using namespace std; const int maxn = ;
int sum[maxn] ; void buildtree(int val,int p) //建树,p为树根的水平位置
{
int vl,vr;
if(val == -) return ;
sum[p] += val ;
cin >> vl ;
buildtree(vl,p - );
cin >> vr ;
buildtree(vr,p + );
} int main(int argc, char *argv[])
{
int k=,v;
while(cin >> v && v != -){
memset(sum,,sizeof(sum)) ;
int pos = maxn/ ;
buildtree(v,pos) ;
int p = ;
while(sum[p] == ) p++ ; //最左边的子叶
cout << "Case " << ++k << ":\n" << sum[p++];
while(sum[p] != ) {
cout << " " << sum[p];
p++;
}
cout << "\n\n";
}
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个单位,右子结点在它右边1个单位.从左向右输出每 ...
- 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 ...
随机推荐
- HDU 1885 Key Task (带门和钥匙的迷宫搜索 bfs+二进制压缩)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Time Limit: 3000/1000 MS (Java/Others) ...
- 【SQLSERVER学习笔记】细节记录
SQLSERVER 查询时,WHERE中使用<>时,不会把NULL值查出来. SQLSERVER子查询中不能使用 ORDER BY. SQLSERVER 使用DISTINCT时,必须把OR ...
- vue+webpack搭建项目
1.全局安装node.js 2.安装vue-cli 可以在项目目录安装 npm install -g vue-cli 使用vue-list命令选择webpack模板 vue init webpack ...
- AppleDoc
使用AppleDoc快速生成iOS开发文档 _ 皮卡丘♪-(´ε` ) 用 appledoc 生成文档 _ Garan no dou xcode-select_ error_ tool 'xcodeb ...
- 不用注解,获取spring容器里的bean(ApplicaitonContext)
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. 使用方法:SpringContextHolder.getBe ...
- md5的理解
md5之所以很难破解,是因为它是不可逆的(下面会解释),它是一种散列函数(哈希函数),并且是单向密码体制,即:从明文到密文的不可逆映射,只有加密过程没有解密过程. 为何说是不可逆映射呢?这是因为,md ...
- Spring Security学习笔记(三)
之前提到过认证后怎么存放用户信息,令牌token是一种方式,session是另一种方式,这里介绍使用spring session data redis存储httpSession. 添加了以上依赖后,我 ...
- Learning Experience of Big Data: Deploying Tomcat 8.0 and connect ssh without password
This mission seems to be easier--we can just decompression Tomcat to our virtural machine and deploy ...
- MAVLink功能开发,移植教程。
MAVLink功能开发 -----------------本文由"智御电子"提供,同时提供视频移植教程,以便电子爱好者交流学习.---------------- 1.MAVLink ...
- vim 配色方案
1. 自己电脑上的vim 注释很难看清,又不想取消高亮.原来显示: 在 if has("syntax") syntax onendif 语句下面追加一句: colorscheme ...