下落的树叶 (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 ...
随机推荐
- Network Security Threats
Network Security Combination of low-cost powerful computing and high-performance networks is a two-e ...
- asp.net mvc5 step by step(一)——CURD增删查改Demo
1. 新建一个项目:
- List、LinkedList、ArrayList、Vector
目前好像写不出比较高质量的随笔,除了多看看别人写的,就是多写,做好自己可以做的 最近听的和看到的最多的一个词就是“勿忘初心”.的确不少人到了一定高度之后,就不知道该怎么做了.(包括我曾经的偶像娜姐,在 ...
- js 变速动画函数
//获取任意一个元素的任意一个属性的当前的值---当前属性的位置值 function getStyle(element, attr) { return window.getComputedStyle ...
- java servlet数据库查询并将数据显示到jsp页面
需要的jar包:mysql-connector-java.jar build path只是个jar包的引用,部署的时候想不丢包最好还是手动拷贝到对应项目的lib文件下. 在try{}中定义的变量为局部 ...
- c和c++单链表
c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...
- (Nagios)-check_hpasm[HP]
Nagios Check_hp HP 2014年11月18日 下午 08:49 https://IP:2381 [root@nagios ~]# tar zxvf check_hp_blad ...
- java读取xml文件的四种方法
Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> ...
- shutil.rmtree()
shutil.rmtree(path, ignore_errors=False, onerror=None) #递归地删除文件 def rmtree(path, ignore_errors=Fal ...
- 纯js轮播图练习-2,js+css旋转木马层叠轮播
基于css3的新属性,加上js的操作,让现在js轮播图花样越来越多. 而现在出现的旋转木马层叠轮播的轮播图样式,却是得到了很多人都喜爱和投入使用. 尤其是在各大软件中,频繁的出现在大家的眼里,在web ...