本题考查点有以下几个:

  1. 对数据输入的熟练掌握
  2. 二叉树的建立
  3. 二叉树的宽度优先遍历

首先,特别提一下第一点,整个题目有相当一部分耗时在了第一个考查点上(虽然有些不必要,因为本应该有更简单的方法)。这道题的输入有以下几种方案:

一次性输入并直接得到要得到的数据

输入后进行加工处理

对于第一种方案,我采用的是与正则相结合的方案

scanf("(%d%[,A-Z]) ",&d,s))

得到这样的写法可谓是费了一番功夫。难点有几个,最突出的是考虑数据不存在的情况:如()(1,)

我的解决方案是对于(1,)读取后边字母时顺带读取 ‘,’这样能避免s为空读取混乱的情况

对于()结束标记的判定,这就需要得到scanf的返回值,返回值为0,即代表读到了()。

还有一点,要知道这道题目可是有多个结束标记,多组数据的,因此呢,

while((k=scanf("(%d%[,A-Z]) ",&d,s))>=0)

while执行的条件是>=0,注意必须包括0,再在循环中判断k==0,k=0即一组数据结束的标记,别忘再加上

scanf("%*s ");

其读取到的是“ )”,至于为啥没有前括号可能是之前已经读了%d之前的那部分括号。


     关于第二个考查点,建立二叉树可以用静态数组来储存节点,也可以动态建立节点。在这里只能用动态来建(数据打大了!)

第三个考查宽度优先遍历,利用队列来完成这个操作。

下面附上AC代码1

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
struct Node{
    Node *left;
    Node *right;
    int value;
    Node():left(NULL),right(NULL){}
};
Node * root;
Node* newnode(){
    Node * t=new Node();
    t->value=0;
    return t;
}
int AddNode(int n, char *s){
    int len = strlen(s);
    Node* node=root;
    for(int i=0;i<len;i++){
        if(s[i] == 'L'){
            if(node->left == NULL){node->left = newnode();}
            node = node ->left;
        }
        else if(s[i] == 'R'){
            if(node->right == NULL)node->right = newnode();
            node = node ->right;
        }
    }
    if(!node->value){node->value=n;return 1;}
    else return 0;
}

bool bfs(vector<int>& ans){
    queue<Node*> q;
    ans.clear();
    q.push(root);
    while(!q.empty()){
        Node* u = q.front(); q.pop();
        if(!u->value)return false;
        ans.push_back(u->value);
        if(u->left != NULL)q.push(u->left);
        if(u->right != NULL)q.push(u->right);
    }
    return true;
}
void remove_tree(Node* u){
    if(u == NULL) return ;
    remove_tree(u->left);
    remove_tree(u->right);
    delete u;
}
int main(){
    #ifdef DEBUG
    freopen("6.7.in","r",stdin);
    freopen("6.7.out","w",stdout);   
    #endif
    root=newnode();
    int d=-1;
    int ok=1;
    int k;
    char s[10]={0};
    while((k=scanf("(%d%[,A-Z]) ",&d,s))>=0){
        if(k==0){
            if(ok) {
                vector<int> ans;
                if(bfs(ans)){
                    int first=1;
                    for(vector<int>::iterator it = ans.begin(); it != ans.end(); ++it){
                        if(!first)printf(" ");
                        else first=0;
                        cout << *it ;
                     }
                     printf("\n");

}
                else ok=0;
            }
            if(!ok)printf("not complete\n");
            ok=1;
            remove_tree(root);
            root=newnode();
            scanf("%*s ");
            continue;
        }
      // printf("(%d%s)\n%d",d,s,k);
        if(!ok)continue;
        if(!AddNode(d,&s[1]))ok=0;
    }

return 0;
}

例题6-7 Trees on the level ,Uva122的更多相关文章

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

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

  2. E - Trees on the level

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

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

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

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

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

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

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

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

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

  7. Uva122 Trees on the level

    Background Trees are fundamental in many branches of computer science. Current state-of-the art para ...

  8. uva-122 Trees on the level(树的遍历)

    题目: 给出一棵树的表示,判断这棵树是否输入正确,如果正确就按层次遍历输出所有的结点,错误的话就输出not complete. 思路: 根据字符串中树的路径先将树建起来,在增加结点和层次遍历树的时候判 ...

  9. 【例题 6-7 UVA - 122 】Trees on the level

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二叉树的话,直接用数组存就好了. 写个bfs记录一下答案. [代码] #include <bits/stdc++.h> ...

随机推荐

  1. 关于Android LayoutInflater的解释

    LayoutInflater的作用就是动态加载xml布局好的界面,类似于findViewById()来获取已经定义好的控件一样.不同点是LayoutInflater是用来找res/layout/下的x ...

  2. LeetCode37 Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  3. C#基础--属性 字段

    访问修饰符: private: 私有成员,在类的内部才可以访问 protected: 受保护的成员,该类内部和继承类的内部可以访问 public: 公共成员, 完全公开, 没有访问限制 interna ...

  4. mysql的相关操作

    查看当前登录用户: mysql> select USER(); +----------------+ | USER() | +----------------+ | root@localhost ...

  5. 段描述符表(GDT+LDT)的有感

    [0]写在前面 要知道,在汇编中,代码的装入顺序决定了在内存中的地址位置.所有的代码或者数据都在硬盘上,当调试或者启动的时候,加载到内存:当需要对数据进行处理的时候,我们通过将数据从内存载入到regi ...

  6. [转]Android 应用的自动升级、更新模块的实现

    本文转自:http://www.oschina.net/question/163910_28462 我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Andro ...

  7. 在centos7中添加一个新用户并授权

    参考地址:http://www.cnblogs.com/woshimrf/p/5906084.html 创建新用户 创建一个用户名为:zhangbiao [root@localhost ~]# add ...

  8. js中Frame框架的属性获取(1)

    js中window和document对象及如何操作iframe 一. window对象 . 什么是window对象? Window对象表示浏览器打开的窗口.如果文档包含iframe或者是frame标签 ...

  9. asp.net解决高并发的方案. (转自网络)

    最近几天一直在读代震军的博客,他是Discuz!NT的设计者,读了他的一系列关于Discuz!NT的架构设计文章,大呼过瘾,特别是Discuz!NT在解决高访问高并发时所设计的一系列方案,本人尤其感兴 ...

  10. Android应用数据备份

    在Android上可以很方便地管理数据备份,那些不慎丢失设备的用户会对该功能感激不尽.备份数据会很安全地存储在云端,并且只在具有相同谷歌ID设备上恢复数据. 下面是典型的AndroidManifest ...