题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=640
题目类型: 数据结构, 二叉树
题目大意:
每年的秋天, 北方的树叶伴随着灿烂无比的颜色, 叶子随风飘落到树下, 地上很快就积累一大堆。
如果同样的事情发生在二叉树, 树上的结点都慢慢落下来, 那该是什么样的景象?
二叉树也是树。
对于一颗二叉树,把每个结点看成一个树枝,结点上的值为那个树枝上的叶子数量。对于每个结点, 它与左儿子和右儿子的距离都是一个单位。
如上图,根结点5与6是在同一垂直线上的, 7和5相距一个单位。
然后, 秋天来了,二叉树的叶子落下了, 而且天气很好,没有风, 所以二叉树的叶子是垂直落下来的。 最后, 同一垂直线的结点的叶子都落在同一堆
上。 让我们求出每一堆上的叶子数量。
解题思路:
题目给出一串数字, 是按照前序遍历给出的, -1表示那个结点为空。
首先可以递归构造出这棵二叉树, 然后再深搜计算出没一堆的数量。
计算时, 我用的技巧是开一个1000大小的数组result, 500坐标位置存根结点, 然后递归时,往左儿子走,坐标-1, 往右儿子走,坐标+1.
这题也可以不用建树, 可以在建树的递归上 直接计算结果。 下面是两个版本的代码。
样例输入:
5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1
样例输出:
Case 1:
7 11 3
Case 2:
9 7 21 15
版本一: 建树版
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int END = -2147483645;
int arr[1000], aSize, result[1000];
class Node{
public:
int data;
Node *father;
Node *left;
Node *right;
};
Node node[1000];
int indexNode, pos;
inline Node* BuildRoot(int n){
indexNode = 1;
node[0].data = n;
node[0].father = NULL;
node[0].left = node[0].right = NULL;
return &node[0];
}
inline Node* NewNode(){
node[indexNode].left = NULL;
node[indexNode].right = NULL;
return &node[indexNode++];
}
Node* build(Node *root){
if(pos>=aSize) return NULL;
if(arr[pos]==-1) {
return NULL;
}
root = NewNode();
root->data = arr[pos];
++pos;
root->left = build(root->left);
++pos;
root->right = build(root->right);
return root;
}
void preOrder(Node *root){
if(root){
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void dfs(Node *root, int index){
if(root){
result[index] += root->data;
// printf("%d: %d, ",root->data, index);
dfs(root->left, index-1);
dfs(root->right, index+1);
}
}
void Solve(){
Node *root=NULL;
pos=0;
indexNode = 0;
root = build(root);
memset(result, 0, sizeof(result));
dfs(root, 500);
int i=0;
while(result[i]==0) ++i;
printf("%d", result[i++]);
for( ; result[i]!=0; ++i)
printf(" %d", result[i]);
printf("\n\n");
}
int main(){
freopen("input.txt","r",stdin);
int cas=1;
while(~scanf("%d", &arr[0]) && arr[0]!=-1){
aSize = 1;
int cnt1=1, cnt2=0;
while(getchar()){
scanf("%d", &arr[aSize++]);
if(arr[aSize-1]==-1)
++cnt2;
else
++cnt1;
if(cnt1+1==cnt2)
break;
}
printf("Case %d:\n", cas++);
Solve();
}
return 0;
}
版本二: 不建树版#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int arr[1000], aSize, result[1000];
int indexNode, pos;
void build(int index){
if(pos>=aSize) return ;
if(arr[pos]==-1) {
return ;
}
result[index] += arr[pos];
++pos;
build(index-1);
++pos;
build(index+1);
}
void Solve(){
pos=0;
indexNode = 0;
memset(result, 0, sizeof(result));
build(500);
int i=0;
while(result[i]==0) ++i;
printf("%d", result[i++]);
for( ; result[i]!=0; ++i)
printf(" %d", result[i]);
printf("\n\n");
}
int main(){
// freopen("input.txt","r",stdin);
int cas=1;
while(~scanf("%d", &arr[0]) && arr[0]!=-1){
aSize = 1;
int cnt1=1, cnt2=0;
while(getchar()){
scanf("%d", &arr[aSize++]);
if(arr[aSize-1]==-1)
++cnt2;
else
++cnt1;
if(cnt1+1==cnt2)
break;
}
printf("Case %d:\n", cas++);
Solve();
}
return 0;
}
—— 生命的意义,在于赋予它意义。
分享到:
相关推荐
html5-canvas-fire-falling.zip
FLUENT-MDM-tut-01_2d-falling-box
落叶 创建落叶的 SVG 动画的 Python 脚本。
人体跌倒检测与追踪使用Tiny-YOLO oneclass检测帧中的每个人,并使用获取骨骼姿势,然后使用模型从每个人跟踪的每30帧中预测动作。 现在支持7种动作:站立,行走,坐着,躺下,站起来,坐下,跌倒。...
fluent模拟小球入水过程的case和程序
Tical是又一款落地障碍游戏,具有可自由调节的场地大小和“环绕模式”,其中场地的左右边界是虚拟连接的。
script src =" canvas-falling-squares.js " > </ script > 在 AMD 加载器中: require ( [ 'canvas-falling-squares' ] , function ( FallingSquares ) { /*…*/ } ) ; 或者使用 Bower: bower install ...
Falling-nes:Falling-NES游戏
关于 这是一个使用 HTML 画布、javascript 和 CSS 实现的简单游戏。 游戏的目标是捕捉尽可能多的星星。 学分
自己使用jq html 编写的《falling_bird 坠落的小鸟》 ,自己原创估计有,逻辑上的bug,给大家一个举一反三的示例,谢谢大家,我的邮箱 lizhilimaster@163.com #### 使用方法:按压空格即可(也可以重新开始) github...
世界正在坠落 我的 MMO 游戏。
计算掉落物体参数的工具 该工具考虑了物体的空气阻力。 如果不是因为空气阻力,则对象将具有线性加速度,事实并非如此。 输入物体的高度,质量和形状,此工具将提供 物体将掉落多长时间 物体的最大速度是多少 ...
Human-detection-and-Tracking-master源码,是一个很不错的Android源码,有兴趣的伙伴们抽时间可以看一下把。
ArcGIS API For JavaScript 字体文件PBF,可本地部署
外研版一起小学英语六下《Module 4Unit 2 The apples are falling down the stairs.》word教案 (2).docx
scratch编程项目源代码文件案例素材-falling for ya meme.zip
语言:English (United States) 在(几乎)任何网页上添加降雪覆盖 在活动选项卡上添加飘落的雪花,以营造良好的冬天感。
ActiveX METEOR Arcade Game. Defend the Earth from falling Meteors. Fast transparent sprite animation and fast collision detection using the (COLLIDE.BAS) collision detection module/engine.
一个著名的游戏,您必须对从窗口顶部掉落的项目进行排序。