【数据结构】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]);
	}
}
时间: 2024-10-16 17:37:42

【数据结构】The Falling Leaves(6-10)的相关文章

H - The Falling Leaves

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 same thing happened to binary trees, how large woul

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

POJ 1577 Falling Leaves 二叉搜索树

HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tr

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>

POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树. 都是两个基本操作解决: 1 二叉树插入操作 2 前序遍历 简单题目了. #include <stdio.h> #include <string> #include <algorithm> #include <vector> using std::vector; using std::string; const int MAX_B = 1024; char buf[MAX_B]; int

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

UVA699 UVALive5471 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 large would the piles