题解:

我的解法是用一个类似字典树结构的结构体来表示节点。看到另一种解法是用数组来映射二叉树的,开到14000就过了,但是我觉得是数据水了,因为题中说最多 256个节点,如果256个节点连成链型,除根节点外每个节点都是父节点的右儿子。那么数组要开pow(2, 256)个。可见这种方法是不可行的;

  • 类似字典树的二叉树

    Accepted 1167 C++ 0 280
    #include "cstdio"
    #include "cstring"
    #include "cstdlib"
    #include "cctype"
    #include "queue"
    #include "vector"
    using namespace std;
    struct Tree {
    int data;
    Tree* lson;
    Tree* rson;
    } *root;
    char s[];
    // v记录遍历的结果,ok记录可否构成树
    vector<int> v;
    bool ok;
    // 初始化节点
    Tree* init() {
    Tree* point = (Tree*)malloc(sizeof(Tree));
    point->data = ;
    point->lson = point->rson = NULL;
    return point;
    }
    // 插入一个节点
    void insert(char* s) {
    int _data = , i = ;
    Tree* point = root;
    while (isdigit(s[i])) {
    _data = _data * + (s[i++] ^ '');
    }
    i++;
    while (s[i] != ')') {
    if (s[i++] == 'L') {
    if (point->lson == NULL) {
    point->lson = init();
    }
    point = point->lson;
    } else {
    if (point->rson == NULL) {
    point->rson = init();
    }
    point = point->rson;
    }
    }
    if (point->data) {
    ok = false;
    }
    point->data = _data;
    }
    // 按层遍历并释放二叉树
    void BFS() {
    queue<Tree*> q;
    q.push(root);
    Tree* point;
    while (!q.empty()) {
    point = q.front();
    q.pop();
    v.push_back(point->data);
    if (!point->data) {
    ok = false;
    }
    if (point->lson) {
    q.push(point->lson);
    }
    if (point->rson) {
    q.push(point->rson);
    }
    free(point);
    }
    }
    int main() {
    while (~scanf("%s", s)) {
    root = init();
    v.clear();
    ok = true;
    while(s[] != '\0') {
    insert(s);
    scanf("%s", s);
    }
    BFS();
    if (!ok) {
    puts("not complete");
    continue;
    }
    printf("%d", v[]);
    for (int i = ; i < v.size(); i++) {
    printf(" %d", v[i]);
    }
    puts("");
    }
    return ;
    }

ZOJ-1167-Trees on the Level的更多相关文章

  1. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  2. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

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

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

  5. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

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

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

  7. 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 ...

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

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

  9. 【ZOJ】3740:Water Level【DP】

    Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especial ...

  10. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

随机推荐

  1. SQL基础教程(第2版)第4章 数据更新:4-4 事务

    ●事务是需要在同一个处理单元中执行的一系列更新处理的集合. ● 事务处理的终止指令包括COMMIT(提交处理)和ROLLBACK(取消处理)两种. ● DBMS的事务具有原子性(Atomicity). ...

  2. Linux相关笔记

    vim下 r /etc/hosts  会把这个文件读进来 r! df -Th  会把执行的内容读取进来 查找 /  ? 替换:s/old/new/g 2到9行替换2,9s/old/new/g 全部替换 ...

  3. C语言-数组的深入学习

    深入学习一下数组1.从内存角度来讲:数组变量就是一次分配多个变量,而且这些变量的地址是连续的,也就是存放这些变量的存储单元是依次相连接的.而且这多个变量必须单独访问,不可以一起访问的.因为他们的地址彼 ...

  4. EXCEL快速实现下拉计算快捷键

    ctrl + shift + 方向键,,选择要填充的范围,,然后ctrl + d

  5. JS专题-FormData

    var formData = new FormData(); <form id="coords" class="coords" onsubmit=&quo ...

  6. windows下隐藏文件夹

    在cmd中找到文件夹所在的路径,然后执行以下命令 隐藏文件:attrib 文件名 +s +h 显示隐藏文件:attrib 文件名 -s -h 后记:attrib指令用于修改文件的属性,文件的常见属性有 ...

  7. C# List引用类型的克隆

    有时候我们想克隆一个List去做别的事,而不影响原来的List,我们直接在list后面加上小点点,发现并没有Clone这样的扩展函数.这时候就只有自己扩展了. 尝试了三种方式,测试都通过了,至于性能方 ...

  8. visual studio2019下动态链接库的制作

    打开visual studio2019创建动态链接库项目,项目名称为20199324dll 然后定义宏:在头文件中定义即可,宏的作用的是允许该函数能够被外部访问,并直接调用.代码如下: // pch. ...

  9. [Algo] 280. Sort With 2 Stacks

    Given an array that is initially stored in one stack, sort it with one additional stacks (total 2 st ...

  10. ubuntu 深度学习cuda环境搭建,docker-nvidia 2019-02

    ubuntu 深度学习cuda环境搭建 ubuntu系统版本 18.04 查看GPU型号(NVS 315 性能很差,比没有强) 首先最好有ssh服务,以下操作都是远程ssh执行 lspci | gre ...