UVa 699:The Falling Leaves 二叉树的落叶

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=640

题目类型: 数据结构, 二叉树

题目大意:

每年的秋天,   北方的树叶伴随着灿烂无比的颜色,    叶子随风飘落到树下, 地上很快就积累一大堆。

如果同样的事情发生在二叉树,   树上的结点都慢慢落下来, 那该是什么样的景象?

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45530.htm

二叉树也是树。

对于一颗二叉树,把每个结点看成一个树枝,结点上的值为那个树枝上的叶子数量。对于每个结点, 它与左儿子和右儿子的距离都是一个单位。

如上图,根结点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;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, printf
, node
, 结点
, root
, 二叉树 php
, result
建树
falling leaves、the falling leaves、leaves are falling、falling leaves读后感、falling leaves 书,以便于您获取更多的相关知识。

时间: 2024-08-31 03:31:24

UVa 699:The Falling Leaves 二叉树的落叶的相关文章

UVa 699 The Falling Leaves:DFS遍历二叉树

699 - The Falling Leaves Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=640 Each year, fall in the North Central region is accompanied by the brilliant colors of the lea

uva 699 The Falling Leaves

点击打开链接 题目意思:给定一串数字,第一个是根节点的值,接下来如果遇到-1 则该点为空,不是-1则创建节点,求最后从左往右每一条竖线的和 分别输出. 解题思路:1 建树 2 前序遍历求和 3 输出 (这里刚误删了,晚了得睡觉了,有时间更新) 注意事项:我们可以采用,把-1 这个节点的val 赋值成很小的数,然后不让它为空,后面计算时候算到这个点直接跳过 代码: #include <cstdio> #include <cctype> #include <cstdlib>

UVa 112:Tree Summing 二叉树构造, 二叉树路径和

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=48&mosmsg=Submission+received+with+ID+10280379 题目类型: 数据结构, 二叉树 加强版样例输入: 22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))

47 个 惊人的 CSS3 动画示例

本文介绍47个令人咋舌的CSS3动画演示汇编.他们展示了CSS3的转换和过渡性的处理.有些是非常有用的,可以作为浏览器的替代品使用.这些效果大多是简单的,看起来很酷.为了尝试这些效果,你需要的WebKit浏览器,如Safari和Chrome. CSS3 Clock With jQuery Analogue Clock 3D Cube That Rotates Using Arrow Keys Multiple 3D Cubes (Slide In/Out) CSS3 Accordion Auto

UVa 11234 Expressions:二叉树 层次遍历 广搜

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2175 题目类型: 数据结构, 二叉树 题目大意: 一般情况下,都是中缀操作符, 如x+y.然后题目给出了一种后缀操作符的形式, 变成 x y +. 进行后缀操作可以用栈模拟,使用push,pop, 过程和经典的"括号匹配"差不

UVa 548:Tree 二叉树的重建——中序遍历与后续遍历进行建树

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 题目类型: 数据结构, 二叉树 题目大意: 给两个组数字,都是在同一棵二叉树上的,第一组是按中序遍历(inorder)顺序输出的,第二组是按后序遍历(postorder)输出的, 根据这两组数据构建出原来的二叉树,然后计算从根结点到每个叶子结

UVa 11234 Expressions:二叉树重建&amp;amp;由叶往根的层次遍历

11234 - Expressions Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2175 Arithmetic expressions are usually written with the operators in between the two operands (which

UVa 10562 Undraw the Trees:二叉树先序遍历

10562 - Undraw the Trees Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=1503 Professor Homer has been reported missing. We suspect that his recent resea

UVa 112 Tree Summing (scanf()去空格&amp;amp;递归&amp;amp;二叉树遍历)

112 - Tree Summing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48 Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one o