699 - The Falling Leaves
来源:互联网 发布:淘宝网马云有多少股份 编辑:程序博客网 时间:2024/05/16 05:50
题目:699 - The Falling Leaves
题目大意:给一棵树,要求算在同一竖直位置的值得和,从左到右打印出来;
解题思路:先建树,然后重点就是调用递归来将同一竖直位置的值加起来,用一个数组来存放,根放在数组最中间,左边的孩子就中间-1,右边的就中间的加1,这样同一竖直的位置的就会加到相同的数组元素里,并且要同时记录最左边的数,以便后面输出时控制最后的回车。
#include<stdio.h>#include<string.h>const int N = 10000;int s[N], m;struct tree {int d; tree *left, *right;tree(int a) { d = a; left = right = NULL;}} *head;void buildtree(tree *&p, int a) {p = new tree(a);scanf("%d", &a);if(a != -1)buildtree(p->left, a);scanf("%d", &a);if(a != -1)buildtree(p->right, a);}void clear(tree *&p) {if(p != NULL) {clear(p->left);clear(p->right);delete(p);p = NULL;}}void handle (tree *p, int k) {if(p != NULL) { s[k] += p->d;if(m < k)m = k;handle(p->left, k -1);handle(p->right, k + 1);}}int main() {int x, n = 0;while(scanf("%d", &x) && x != -1) {n++;m = 0;printf("Case %d:\n", n);buildtree(head, x);memset(s, 0, sizeof(s));handle(head, N/2);for(int i = 0; i <= m; i++)if(s[i]) {if(i != m)printf("%d ",s[i]);elseprintf("%d\n\n", s[i]);}clear(head);}return 0;}
0 0
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- 699 - The Falling Leaves
- uva 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- 699 - The Falling Leaves---递归
- UVA 699 - The Falling Leaves
- 699_uva The Falling Leaves
- UVa 699 - The Falling Leaves
- uva 699 The Falling Leaves
- 链表剖析之双向链表剖析
- Kinect介绍和开发环境配置
- 書劍恩仇錄 (無線電視翡翠台版本) - 前一章的愛情橋段
- CocoStudio UI编辑器 学习总结
- Cocos2d-x 3.0 开发(四)使用CocoStudio创建UI并载入到程序中
- 699 - The Falling Leaves
- Leetcode全矩阵问题
- 开源hash代码uthash的原理与用法
- 【C++基础】const成员函数
- lua的数据结构 -- 数组
- 学习开发之常复习
- 最大子序列和问题
- 取石子(七)
- HDOJ-1166