Trees on the level UVA - 122 

解题思路:

  首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束。因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环。

  接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是‘L’就往左走,走不动时建一颗新树,同样的方法处理右子树,最后读入结点值。由于输入可能有误,因此用一个全局变量failed来记录是否有输入错误的情况出现,如果在建树过程中发现该结点已经被赋过值,那么全局变量failed变为true。

  最后开始BFS找结点值,此时可能出现有结点没有结点值的情况,因此要把bfs定义为bool型,只要出现这种非法情况,返回false。最后便不难根据情况进行输出了。

 sscanf() - 从一个字符串中读进与指定格式相符的数据.

  函数原型:
  Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
  int scanf( const char *format [,argument]... );

  说明:
  sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
  其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' | 非%符号}
  注:
  1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)
  2、{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。
  3、width表示读取宽度。
  4、{h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。
  5、type :这就很多了,就是%s,%d之类。
  6、特别的:%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值
  支持集合操作:
  %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
  %[aB'] 匹配a、B、'中一员,贪婪性
  %[^a] 匹配非a的任意字符,贪婪性

 #include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std; const int maxn = ;
vector<int> ans;
bool failed;
class Node{
public:
bool create;//表示该结点是否已经建立
int v;//结点值
Node *left,*right;
Node():create(false),left(NULL),right(NULL){}//初始化
}*root;//根节点
char a[maxn];//保存读入的节点 void create_tree(int v,char *tr)
{
int len = strlen(tr);
Node *temp = root;
if(len == )
{
temp->v = v;
temp->create = true;
return;
}
for(int i=;i<len-;i++)///注意最后一个是')'
{
if(tr[i] == 'L')///向左子树
{
if(temp->left == NULL)
{
temp->left = new Node;
}
temp = temp->left;
}
else{
if(temp->right == NULL)
temp->right = new Node;
temp = temp->right;
}
}
if(temp->create == true) failed = true;///已经赋值,说明输入有误
temp->v = v;
temp->create = true;
} bool read_input()
{
failed = false;
root = new Node;///创建根节点
for(;;)
{
if(scanf("%s",a) != ) return false;///整个输入结束
if(!strcmp(a,"()")) break;///读到结束标志,退出循环
int v;
sscanf(&a[],"%d",&v);
create_tree(v,strchr(a,',')+);
}
return true;
}
int print_tree()
{
queue<Node*> q;
ans.clear();///清空 vector<int> ans;
q.push(root);///初始时只有一个根节点
while(!q.empty())
{
Node *temp = q.front();q.pop();
if(temp->create == false)///没有被建立的结点
return ;
ans.push_back(temp->v);
if(temp->left != NULL) q.push(temp->left);
if(temp->right != NULL) q.push(temp->right);
}
return ;
} int main()
{
while(read_input())//有输入则建树
{
///开始遍历树,层次输出
int temp = print_tree();
if(temp == || failed)
cout<<"not complete"<<endl;
else{
int len = ans.size();
for(int i=;i<len;i++)
{
cout<<ans[i];
if(i == len-) cout<<endl;
else cout<<" ";
}
}
}
return ;
}

UVA 122 -- Trees on the level (二叉树 BFS)的更多相关文章

  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(二叉树层序遍历)

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

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

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

  4. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

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

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

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

  7. UVa 122 Trees on the level

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

  8. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

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

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

随机推荐

  1. Date对象中的方法

    特殊说明:设置时间的方法,虽然W3C说明传参的范围,在开发过程中,传入的参数不在该范围也是可以的.例如: var t = new Date(), d = t.getDate(); //当天时间往前推2 ...

  2. RAD Studio 10 up1欢迎页证书不可用

    不只是欢迎页,每打开一个新的工程,都会出现上面那个证书不可用的提示. 解决方法: 1.通过Fiddler网络监控软件分析发现,出现这个问题的原因是bds启动的时候会用https协议访问谷歌的统计服务器 ...

  3. TensorFlow入门——MNIST初探

    import tensorflow.examples.tutorials.mnist.input_data as input_data import tensorflow as tf mnist = ...

  4. Addthis分享插件后url乱码的解决办法

    Addthis分享插件安装后,有时候URL后面会出现类似#.VB4mxhbjtnQ的一串乱码的乱码,作用是用来追踪客户客户,但是给客户的印象会以为木马中毒之类的 http://localhost/mi ...

  5. 树形DP Choosing Capital for Treeland

    给你一棵有向树,需要选定一个点为capital,满足翻转边数最小 思路:先求出1为capital 的答案,然后向下更新孩子节点 dp[i]=dp[i-1]+judge(i); #include< ...

  6. DB2中常见sqlCode原因分析

    000 | 00000 | SQL语句成功完成 01xxx | SQL语句成功完成,但是有警告 +012 | 01545 | 未限定的列名被解释为一个有相互关系的引用 +098 | 01568 | 动 ...

  7. hiho #1038 : 01背包 (dp)

    #1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...

  8. qt5---步长调节器SpinBox和QDoubleSpinBox

    #include <QSpinBox>            #include <QDoubleSpinBox> QSpinBox 用于整数的显示和输入,一般显示十进制数,也可 ...

  9. Python3数据结构汇总

    字符 列表 元组 集合 字典 能否被索引或切片 能 能 能 否 否 元素能否被编辑 否 能 否 能 能 增 1.list.append(x):把一个元素添加到列表的结尾: 2.list.insert( ...

  10. django admin登陆页出现TypeError at /admin/

    出现此错误的原因主要是,添加多条URL时urlpatterns后面的序列符号写错了,检查所有的urls.py文件将{}改为[]. error: urlpatterns = {} right: urlp ...