题解:

我的解法是用一个类似字典树结构的结构体来表示节点。看到另一种解法是用数组来映射二叉树的,开到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. tensorflow 分布式训练

    TF实现分布式流程 1.创建集群 ClusterSpec & Server cluster = tf.train.ClusterSpec({"ps": ps_hosts, ...

  2. 统计web 访问日志的请求数据

    tomcat日志格式  在配置文件 server.xml 中,具体参照官方文档 https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#A ...

  3. 和我一起从0学算法(C语言版)(四)

    第三章 搜索 深度优先搜索与宽度优先搜索 定义 深度优先搜索(DFS) 过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 宽度优先搜索(BFS) 不考虑结果的可能位 ...

  4. Oracle与MySQL的区别对比

    本文对数据库Oracle与MySQL进行了区别对比,其中从并发性.一致性.事务.数据持久性等十三方面进行了对比. 本文摘自 51cto 一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源 ...

  5. 3)在View中添加LBUTTONDOWN(标准消息)

    1)消息一共分为四类: (1)标准消息-->以WM_  开头的都是标准消息 (2)命令消息---->  菜单  工具条  快捷键(两个按键的组合是快捷键,一个按键是 WM_KEYDOWN( ...

  6. js中call和apply的实现原理

    js中call和apply的实现原理            实现call的思路: /* 还有就是call方法是放在Function().prototype上的也就是构造函数才有的call方法 (我门可 ...

  7. sybase连接失败 JZ006: Caught IOException: java.net.ConnectException处理方式

    windows系统下的处理办法: 1.查找端口为5000的进程的pid: 在cmd窗口中输入 netstat -ano 我这里是2324. 打开任务管理器,找到pid是2324的进程,结束进程. 打开 ...

  8. ZJNU 1535 - 新建的大楼--中高级

    因为从俯视图看,输入输出的视角是从右下方看向左上方的 所以左上角的正方体最有可能被其他正方体挡住 立体上,底部的正方体最有可能被顶部的正方体挡住 所以绘图应该从后往前,从下往上绘制 剩下的就是一大堆计 ...

  9. C语言 指针理解

    1.指针 指针全称是指针变量,其实质是C语言的一种变量.这种变量比较特殊,通常他的值会被赋值为某个变量的地址值(p = &a),然后我们可以使用 *p 这样的方式去间接访问p所指向的那个变量. ...

  10. github新手使用教程

    1.首先打开https://github.com/官网 注册一个github账号 2.注册成功之后,登录账号,创建一个属于自己的库 3.创建完成之后,为了方便电脑上的代码上传到github 仓库上,要 ...