题意  :输入一棵二叉树,你的任务是按从上到下、从左到右的顺序输出各个结点的值。每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右)。在输入中,每个结点的左 括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括 号“()”结束(这对括号本身不代表一个结点),注意,如果从根到某个叶结点的路径上有的结点没有在输入中给出,或者给出超过一 次,应当输出not complete。结点个数不超过256。

分析  : 如果使用数组建树的话,256个结点看着不多,但是如果全部的节点都连成一条线的话数组是不足以存储的,所以采用指针链表的方式存储,动态建树。这道题还需要输出无法建造出树的情况,而一般的动态建树都是从根结点自上而下的建立,判断就变得些许麻烦了。如果从输入考虑的话,可以先按某种方式排序,使得输入和建树顺序是一致的,那么判断就方便了,这里紫书用了一个更简便的方法,直接在根节点的结构里面定义一个布尔数组来判断这个节点是否已经赋值,那么在建树的过程当中,不管当前这个点是否合法(其父节点已经存在),先全部动态分配内存建立起来,判断此树是否合法的工作只要在层序遍历时判断每个节点的布尔值即可。层序遍历则可以想象成BFS广搜在搜索解答树的过程,利用队列即可完成。

瞎 :

① strchr(char *, char) 即找到char *数组中第一次出现char的位置,类似于string::find_first_of()

②题目输入结束的条件是EOF,这时候不要轻易用while(1){...},超时也有可能是这样的输入导致,考虑将输入变成函数 bool read(){..}然后while(read()){...}来解决。

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    bool Have_val;
    int val;
    Node * Left, * Right;
    Node():Have_val(false),Left(NULL),Right(NULL){}
};
];
Node * root;
bool Failed;
Node * newnode() { return new Node(); }
inline void Addnode(int num, char * s)
{
    int len = strlen(s);
    Node * u = root;
    ; i<len; i++){
        if(s[i] == 'L'){
            if(u->Left==NULL) u->Left = newnode();
            u = u->Left;
        }
        else if(s[i] == 'R'){
            if(u->Right==NULL) u->Right = newnode();
            u = u->Right;
        }
    }
    if(u->Have_val) Failed = true;
    u->val = num;
    u->Have_val = true;
}
]; ;
inline void bfs()
{
    queue<Node *> q;
    q.push(root);
    while(!q.empty()){
        Node * temp = q.front();
        q.pop();
        if(!temp->Have_val){
            Failed = true;
            return ;
        }
        ans[top++] = temp->val;
        if(temp->Left!=NULL) q.push(temp->Left);
        if(temp->Right!=NULL) q.push(temp->Right);
    }
}
inline void destroy(Node * u)
{
    if(u==NULL) return ;
    destroy(u->Left);
    destroy(u->Right);
    delete u;
}
bool read()
{
    Failed = false;
    destroy(root);
    root = newnode();
    while(true){
        ) return false;
        if(!strcmp(s, "()")) break;
        int v;
        sscanf(&s[], "%d", &v);
        Addnode(v, strchr(s, );
    }
    return true;
}
int main(void)
{
    while(read()){
        top = ;
        bfs();
        if(Failed) puts("not complete");
        else{
            ; i<top-; i++)
                printf("%d ", ans[i]);
            printf(]);
        }
    }
    ;
}

UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)的更多相关文章

  1. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  2. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  3. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  4. uva 122 trees on the level——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

  5. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  6. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  7. UVa 122 Trees on the level

    题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...

  8. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

  9. Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>

    问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

随机推荐

  1. maven坐标Dependencies和Exclusions详解

    1.概念介绍 Dependencies:是可选依赖(Optional Dependencies) Exclusions:是依赖排除(Dependency Exclusions) 2.Dependenc ...

  2. 洛谷 P4779 单源最短路径(标准版) 题解

    题面 这道题就是标准的堆优化dijkstra: 注意堆优化的dijkstra在出队时判断vis,而不是在更新时判断vis #include <bits/stdc++.h> using na ...

  3. c++ Convert struct to bytes

    D:\stock\Tskingfromgoogle\src\NetTS\TW.cpp Convert struct  to bytes //Convert struct to bytes 2019/0 ...

  4. Android API文档

    官方API文档: Android官网: https://developer.android.google.cn/index.html (不需要梯子) Android官网: https://develo ...

  5. Layui关闭弹出层并刷新父窗口

    先确保已经引入layui和jquery 再确保初始化layer弹出层 <script> layui.use(['form', 'layer'], function() { var form ...

  6. sql server 平方根函数SQRT(x)

    --SQRT(x)返回非负数x的二次方根 示例:select  SQRT(9), SQRT(36); 结果:3    6

  7. 关于chrome请求被挂起页面加载缓慢问题的追查

    请参考FEX团队探究结果 http://fex.baidu.com/blog/2015/01/chrome-stalled-problem-resolving-process/ 结论如下: 请求成功构 ...

  8. [转载]神经网络偏置项(bias)的设置及作用

    [转载]神经网络偏置项(bias)的设置及作用 原文来自:https://www.cnblogs.com/shuaishuaidefeizhu/p/6832541.html 1.什么是bias? 偏置 ...

  9. springboot热部署设置

    springboot提供了热部署,所谓热部署就是当你修改了文件代码,不用重新去启动服务器,而你只要重新build一下当前项目就可以重新编译了.而这就是热部署. 其实springboot热部署就是通过一 ...

  10. JavaSE基础:泛型

    泛型 1.引入 情景模式描述,假设完成一个学生的成绩的情况: 整数: math=80,english=70 小数: math=85.6,englisth=77.8 字符串: math="66 ...