UVa699 The Falling Leaves
// UVa699 The Falling Leaves
// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空
树
// UVa699 The Falling Leaves
// Rujia Liu
// 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空树
// 算法:在“建树”的同时计算,无须真正的把树保存下来 #include<cstring>
#include<iostream>
using namespace std; const int maxn = 200;
int sum[maxn]; // 输入并统计一棵子树,树根水平位置为p
void build(int p) {
int v;
cin >> v;
if(v == -1) return; // 空树
sum[p] += v;
build(p - 1);
build(p + 1);
} // 边读入边统计
bool init() {
int v;
cin >> v;
if(v == -1) return false; memset(sum, 0, sizeof(sum));
int pos = maxn/2; // 树根的水平位置
sum[pos] = v;
build(pos - 1); // 左子树
build(pos + 1); // 右子树
return true;
} int main() {
int kase = 0;
while(init()) {
int p = 0;
while(sum[p] == 0) p++; // 找最左边的叶子 // 开始输出。因为要避免行末多余空格,所以稍微麻烦一点
cout << "Case " << ++kase << ":\n" << sum[p++];
while(sum[p] != 0) {
cout << " " << sum[p];
p++;
}
cout << "\n\n";
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; struct Node
{
Node* l;
Node* r;
int v;
int index;
}; Node* root; int min_index;
int max_index;
int ans[10000]; Node* dfs(int index)
{
int i;
cin>>i;
//cout<<i<<endl;
if(i==-1) return 0;
Node* root=new Node;
root->v=i;
root->index=index;
if(index<min_index)
min_index=index;
if(index>max_index)
max_index=index;
root->l=dfs(index-1);
root->r=dfs(index+1);
return root;
} void delete_tree(Node* root)
{
if(!root)
return;
delete_tree(root->l);
delete_tree(root->r);
delete root;
} void bfs()
{
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* nd=q.front(); q.pop();
ans[nd->index - min_index]+=nd->v; //cout<<nd->v<<" ";
if(nd->l) q.push(nd->l);
if(nd->r) q.push(nd->r);
} } void output()
{
int i;
for(i=0;i<max_index-min_index;i++)
{
cout<<ans[i]<<" ";
}
cout<<ans[i]<<endl<<endl;;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva699.in", "r", stdin);
#endif
int kase=1;
while(root=dfs(0))
{
cout<<"Case "<<kase++<<":"<<endl;
bfs();
output();
delete_tree(root);
min_index=max_index=0;
memset(ans, 0, sizeof(ans));
} return 0;
}
UVa699 The Falling Leaves的更多相关文章
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- 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 ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
随机推荐
- version_info
import sys def check_version(): v = sys.version_info if v.major == 3 and v.minor >= 4: return Tru ...
- Java [Leetcode 88]CMerge Sorted Array
题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...
- Java [leetcode 4] Median of Two Sorted Arrays
问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- [端API] 控件在一个页面里open了,但其他页面打开这个控件怎么关闭
加在控件的参数里<script type="text/javascript" src="../script/api.js"></script& ...
- win7和centos双系统安装
几年之前为了安装xp和linux的双系统曾折腾了好多天,今天为了安装这个win7和centos双系统,也折腾了两天多,哦,我的天,安装个双系统,怎么这么麻烦呢? 没有来得及整理,先铺上草稿,供同志们参 ...
- [Papers]NSE, $u_3$, Lebesgue space [Kukavica-Ziane, Nonlinearity, 2006]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{5}{8},\quad \frac{24}{5}<q ...
- android 滑动菜单SlidingMenu的实现
首先我们看下面视图: 这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对Gesture ...
- C++ 出现bug :二位数组的操作运算,求非对角线的元素的和
编写一个通用程序,求出二位数组(行数和列数必须相等)的非对角线的元素之和,试建立类MATRIX完成上述功能 #include<iostream> using namespace std; ...
- C++中的重载,隐藏,覆盖,虚函数,多态浅析
直到今日,才发现自己对重载的认识长时间以来都是错误的.幸亏现在得以纠正,真的是恐怖万分,雷人至极.一直以来,我认为重载可以发生在基类和派生类之间,例如: class A { public: void ...
- Spring学习笔记(一) Spring基础IOC、AOP
1. 注入类型 a) Spring_0300_IOC_Injection_Type b) setter(重要) c) 构造方法(可以忘记) d) ...