The Falling Leaves UVA 699

说说:

这道题非常简单,本质上就是二叉树的先序遍历。只需要建立一个数组,然后将初始位置放在数组中心。然后进入左子树的根节点,向数组左侧移动一位,添加当前节点所含的值,同理进入右子树的根节点,向数组右侧移动一位,添加当前节点所含的值。并标记好到达过的数组的左右边界,最后将边界内数组的值输出即可。

源代码:

#include <stdio.h>
#include <string.h>
#define MAXN 200

int piles[MAXN];
int leftmost,rightmost;

void tree(int pos);

int main(){
  int val,T=1,i,mid;
  char c;
 //freopen("data","r",stdin);
  while(1){
   scanf("%d%c",&val,&c);
   if(val==-1&&c==' '){//空树,但并未结束
    while(scanf("%d%c",&val,&c)&&c!='\n');
    continue;
    }
   else if(val==-1)//测试结束
     break;

   memset(piles,0,sizeof(piles));
   leftmost=rightmost=mid=100;//从数组的中间开始

   piles[mid]=val;

   tree(mid-1);//遍历左子树
   tree(mid+1);

   printf("Case %d:\n",T++);
   for(i=leftmost;i<=rightmost;i++)
     printf("%d%c",piles[i],i==rightmost?'\n':' ');

   putchar('\n');
  }

  return 0;
}

void tree(int pos){
  int val;

  scanf("%d",&val);
  if(val==-1)
    return;
  else{
   piles[pos]+=val;
   leftmost=pos<leftmost?pos:leftmost;//更新左边界
   rightmost=pos>rightmost?pos:rightmost;
  }

  tree(pos-1);
  tree(pos+1);

  return ;
}
时间: 2024-08-07 06:20:27

The Falling Leaves UVA 699的相关文章

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

题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与天平那题不同,本题不需要构造出完整的结点左右子树,只需要构造出结点的相对位置,每次输入一个结点树,若为-1,则返回,否则依次递归执行input(p-1)与input(p+1). 代码如下: 1 #include<stdio.h> 2 #include<cstring> 3 #inclu

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

题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新leftson ,更新rightson; 代码: #define _CRT_SECURE_NO_WARNINGS #include "stdio.h" #include<stdio.h> #include<algorithm> #include<string>

下落的树叶 (The Falling Leaves UVA - 699)

题目描述: 原题:https://vjudge.net/problem/UVA-699 题目思路: 1.依旧二叉树的DFS 2.建树过程中开个数组统计 //紫书源代码WA AC代码: 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int maxn = 100 ; 6 int sum[maxn] ; 7 8 void buildtree(int val,int p) //建树,p

[2016-02-09][UVA][699][The Falling Leaves]

时间:2016-02-09 13:29:10 星期二 题目编号:UVA 699 题目大意:  给一棵树,每棵树有一个叶子,叶子的值是点权,求叶子垂直落下后, (同一个方向的形成一堆),求每堆叶子的总权值 位置的描述:每个左子树在根左边一个单位,右子树在根右边一个单位 分析: 遍历一遍二叉树,传参保存每个二叉树的位置,最后保存即可 每行不超过80个字符,那么节点数不大于80个,堆数不大于80个 方法:dfs,递归 解题过程遇到问题: 一行数据不一定就是一颗完整的树!!!!!!! 开始还以为读取到第

UVa 699.The Falling Leaves【7月23】

The Falling Leaves Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how lar

UVA 699 The Falling Leaves

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<map> #include<algorithm> #include<cctype>

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

本文纯属原创,转载请注明出处,谢谢.http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the sam

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s

UVA 399 The Falling Leaves(二叉树)

 The Falling Leaves  Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how l