题目描述:

原题:https://vjudge.net/problem/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)的更多相关文章

  1. 二叉树的递归遍历 The Falling Leaves UVa 699

    题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...

  2. 【紫书】 The Falling Leaves UVA - 699 递归得简单

    题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新le ...

  3. The Falling Leaves UVA - 699

    题目链接:https://vjudge.net/problem/UVA-699 题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位.从左向右输出每 ...

  4. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  5. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  6. UVA 699 The Falling Leaves (二叉树水题)

    本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...

  7. UVa 699 The Falling Leaves (树水题)

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

  8. UVA - 699The Falling Leaves(递归先序二叉树)

    The Falling Leaves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  9. The Falling Leaves(建树方法)

    uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...

随机推荐

  1. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  2. 【MongoDB】CentOS上安装MongoDB

    权限部分尚未测试完成,请勿参考. 1.去官方网站下载Mongodb for linux的包,我没找到CentOS的,随便下载了个mongodb-linux-x86_64-amazon-3.2.0.tg ...

  3. C#自定义异常

    继承自System.ApplicationException类,并使用Exception作为自定义异常类名的结尾 三个构造函数:一个无参构造函数:一个字符串参数的构造函数:一个字符串参数,一个内部异常 ...

  4. 集合Gk表示这样一堆数字,该集合内的数字有k个1

    问题描述 集合Gk表示这样一堆数字,该集合内的数字有k个1.比如,G1 = { 1, 10, 100, 1000, ...} G2 = {11, 110, 1110 }, ... , Gk { ... ...

  5. mongo配置项说明

    mongo configure 配置文件   storage:     dbPath: mongod实例存储其数据的目录.     indexBuildRetry: 指定是否mongod在下次启动时重 ...

  6. 分布式版本控制系统之Git

    Git Git 是目前世界上最先进的分布式版本控制系统(没有之一) 作用 源代码管理 为什么要进行源代码管理? 方便多人协同开发 方便版本控制 Git的诞生 作者是 Linux 之父:Linus Be ...

  7. 利用ascii码生成26个英文字母

    <script> let a = ""; for (var i = 65; i < 91; i++) { a += String.fromCharCode(i); ...

  8. Java Web项目里 classpath 具体指哪个路径

    classpath路径指什么 只知道把配置文件如:mybatis.xml.spring-web.xml.applicationContext.xml等放到src目录(就是存放代码.java文件的目录) ...

  9. KEIL MDK-ARM Version 5.26正式版开发工具下载

    Keil MDK最新版本已经出来啦,ARM开发工具MDK-ARM Version 5.26地址:http://www.myir-tech.com/soft.asp?id=1141,需要的可以去下载哦 ...

  10. python拓展应用:运行do文件及其衍生内容

    #-*-coding:utf8-*- import os import subprocess import shutil import psutil import time #查找指定文件名称和查找范 ...