题意  :输入一棵二叉树,你的任务是按从上到下、从左到右的顺序输出各个结点的值。每个结 点都按照从根结点到它的移动序列给出(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. 【神经网络与深度学习】【CUDA开发】【VS开发】Caffe+VS2013+CUDA7.5+cuDNN配置过程说明

    [神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置过程说明 标签:[Qt开发] 说明:这个工具在Windows上的配置真的是让我纠结万分,大部分 ...

  2. IDEA 一次启动多个微服务模块项目

    1,打开IDEA项目 .idea 下 的workspace.xml 2,查找“RunDashboard” 节点 3,添加如下内容 <option name="configuration ...

  3. Linux内核基础优化

    Linux内核基础优化 net.ipv4.ip_forward = 1 #开启网络转发 net.ipv4.conf.default.rp_filter = 0 #开启代理功能 net.ipv4.con ...

  4. Java Android 开发数字不足位数前面补0

    import java.text.DecimalFormat; public void changeColor(View view) { DecimalFormat decimalFormat = n ...

  5. 细说vue axios登录请求拦截器

    当我们在做接口请求时,比如判断登录超时时候,通常是接口返回一个特定的错误码,那如果我们每个接口都去判断一个耗时耗力,这个时候我们可以用拦截器去进行统一的http请求拦截. 1.安装配置axios cn ...

  6. vue在组件中使用v-model

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. js 学习二 字符串常用方法

    1.字符串长度 string.length var browserType = 'mozilla'; browserType.length; //7 2在字符串中查找子字符串 string.index ...

  8. 利用ARouter实现组件间通信,解决子模块调用主模块问题

    如果你还没使用过ARouter请你按照这篇下面博客尝试使用下然后再往下看组件通信的内容(不然的话可能会懵逼)Android Studio接入ARouter以及简单使用 如果你使用过ARouter请继续 ...

  9. MSDN上对yield关键字的示例

    public class PowersOf2 { static void Main() { // Display powers of 2 up to the exponent of 8: , )) { ...

  10. String与C风格字符串转换

    String字符串转换为C风格字符串需要利用string类的成员函数c_str().而C风格字符串转换转换为string字符串可以直接利用运算符=.首先介绍c_str()函数原型: const val ...