UVa699 - The Falling Leaves

题意

给一棵二叉树,左子结点在父结点左边一个单位,右子节点在父节点的右边一个单位,按先序遍历的方式输入一棵树,-1为空结点,输出每列结点权值的和。

思路

递归建树

借了别人画的一个图

总结

目前还不怎么会二叉树_(:з」∠)_只能照着书上写一遍,现在做到的只是能理解,还不能自己写出这个

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 typedef long long LL;
 6 const int maxn = 200;
 7 int sum[maxn];
 8 using namespace std;
 9 void build(int p)
10 {
11     int v;
12     cin >> v;
13     if(v == -1) return;
14     sum[p] += v; //每一列
15     build(p - 1);
16     build(p + 1);
17 }
18 bool init()
19 {
20     int v;
21     cin >> v;
22     if(v == -1) return false;
23     memset(sum,0,sizeof sum);
24     int pos = maxn / 2;
25     sum[pos] = v;
26     build(pos - 1);
27     build(pos + 1);
28 }
29 int main()
30 {
31     int kase = 0;
32     while(init()) {
33         int p = 0;
34         while(sum[p] == 0) p++; //找到最左边的一列
35         printf("Case %d:\n%d",++kase,sum[p++]);
36         while(sum[p])
37             printf(" %d",sum[p++]);
38         printf("\n\n");
39     }
40     return 0;
41 }
时间: 2024-10-05 02:26:30

UVa699 - The Falling Leaves的相关文章

【数据结构】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> #in

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>

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

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

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

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

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