uva 699 The Falling Leaves

点击打开链接

题目意思:给定一串数字,第一个是根节点的值,接下来如果遇到-1 则该点为空,不是-1则创建节点,求最后从左往右每一条竖线的和 分别输出。

解题思路:1 建树 2 前序遍历求和 3 输出 (这里刚误删了,晚了得睡觉了,有时间更新)

注意事项:我们可以采用,把-1 这个节点的val 赋值成很小的数,然后不让它为空,后面计算时候算到这个点直接跳过

代码:

#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
const int MIN  = -999999999;

int num[MAXN];
int ans[1000];
int mark;//用来标记是否创建过

//二叉树的结构体
struct Btree{
    int val;
    struct Btree *lchild;
    struct Btree *rchild;
    struct Btree *father;
};
Btree *root;//根节点
Btree *cur;//当前节点
Btree *temp;//临时用的

//根节点的初始化
void init(Btree *u , int n){
    u -> father = NULL;
    u -> lchild = NULL;
    u -> rchild = NULL;
    u -> val = n;
}
//增加新的节点
void creatnode(int tempval){
    temp = new Btree;//分配空间
    mark++;
    if(tempval == -1){//如果为-1
        init(temp , MIN);
        if(cur -> lchild == NULL){ //左子树为空
            cur -> lchild  = temp;
            cur -> lchild != NULL;
        }
        else{
            cur -> rchild   = temp;
            cur -> rchild  != NULL;
            while(cur->rchild != NULL && cur != root)//退回去,注意要找到它的右子树为空
                cur = cur -> father;
        }
    }
    else{//不是-1
        init(temp , tempval);//初始化
        if(cur -> lchild == NULL) {//左子树为空
            cur -> lchild = temp;
            temp = cur;
            cur  = cur -> lchild;
        }
        else{
            cur -> rchild = temp;
            temp = cur;
            cur  = cur -> rchild;
        }
        cur -> father = temp;//当前节点重新赋值
    }
}

//前序遍历求和
void preorder(Btree *u , int p){
    if(u!=NULL){
        if(u -> val != MIN)//只要不是MIN就相加
           ans[p] += u->val;
        preorder(u->lchild , p-1);//这里不要写p--;
        preorder(u->rchild , p+1);//这里不要写p++;
    }
}

//输出函数
void output(){
    int i , j;
    for(i = 0 ;i < 1000 ; i++){
        if(ans[i] != 0)
            cout<<ans[i];
        if(ans[i+1] != 0)
            cout<<" ";
    }
    cout<<endl<<endl;
}

//主函数
int main(){
    int i ,j , k = 1 ,rootnum , nodenum;
    while(cin>>rootnum && rootnum != -1){
        root = new Btree;//产生根节点
        init(root , rootnum);//初始化
        cur = root;//当前节点为根节点
        mark = 0;
        while(1){
            scanf("%d" , &nodenum);//输入节点值
            creatnode(nodenum);
            if(nodenum == -1 && cur == root && mark != 0 && cur->lchild != NULL && cur->rchild != NULL)//停止输入情况
                break;
        }
        memset(ans , 0 , sizeof(ans));
        preorder(root , 500);//前序遍历求和
        printf("Case %d:" , k);
        output();
        k++;
    }
}
时间: 2024-08-02 21:19:13

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 二叉树的落叶

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=640 题目类型: 数据结构, 二叉树 题目大意: 每年的秋天,   北方的树叶伴随着灿烂无比的颜色,    叶子随风飘落到树下, 地上很快就积累一大堆. 如果同样的事情发生在二叉树,   树上的结点都慢慢落下来, 那该是什么样的景象? 本文URL地

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 369 Combinations:如何用double算组合数

369 - Combinations Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=305 Computing the exact number of ways that N things can be taken M at a time can be a

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d

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

UVa 573 The Snail (模拟)

573 - The Snail Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=514 A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can

UVa 128 Software CRC:模计算及CRC循环冗余校验码

128 - Software CRCTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=64 You work for a company which uses lots of personal computers. Your boss, Dr Penny Pi

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.