题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.

For example, a level order traversal of the tree

is: 5, 4, 8, 11, 13, 4, 7, 2, 1.

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

 #include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int lch,rch,val;
bool b;
}a[],n1,n2;
int n,ans[];
int rd()
{
int i,j,k,p,x,y,z,rt;
char s[],c1,c2;
memset(a,,sizeof(a));
n=;
if (scanf("%s",s)==-) return ;
rt=;
while ()
{
if (s[]==')') break;
x=;
for (i=;s[i]!=',';i++)
x=x*+s[i]-'';
p=;
for (i=i+;i<=strlen(s)-;i++)
if (s[i]=='L')
{
if (!a[p].lch) a[p].lch=++n;
p=a[p].lch;
}
else
{
if (!a[p].rch) a[p].rch=++n;
p=a[p].rch;
}
if (a[p].b) rt=-;
a[p].val=x;
a[p].b=;
scanf("%s",s);
}
return rt;
}
queue<int> q;
int main()
{
int i,j,k,l,m,p,x,y,z;
bool b;
while ()
{
x=rd();
if (!x) break;
if (x==-)
{
printf("not complete\n");
continue;
}
while (!q.empty()) q.pop();
q.push();
memset(ans,,sizeof(ans));
k=b=;
while (!q.empty())
{
n1=a[q.front()];
q.pop();
if (!n1.b)
{
b=;
break;
}
ans[++k]=n1.val;
if (n1.lch) q.push(n1.lch);
if (n1.rch) q.push(n1.rch);
}
if (b)
printf("not complete\n");
else
{
printf("%d",ans[]);
for (i=;i<=k;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
}

如果直接用数组下标表示位置,那么当所有节点连成一条线的时候下标将变得很大。所以需要在每个节点记录他的左右儿子位置,这样可以节省空出来的空间。

每个节点用一个bool记录是否被赋过值,如果没被赋过值或是被赋第二次值,那就not complete了。

但是注意的细节就是由于多组数据,即使读入时已经知道not complete也要读完。

uva 122 trees on the level——yhx的更多相关文章

  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. UVa 122 Trees on the level(二叉树层序遍历)

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

  4. UVa 122 Trees on the level

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

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

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

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

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

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

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

  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. OAuth2.0 基础概述

    web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...

  2. 邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    graph.c #include <stdio.h> #include <stdlib.h> #include <limits.h> #include " ...

  3. ex_KMP--Theme Section

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/B Description It's time f ...

  4. jquery TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [jquery.js:1904]错误原因

    今天,某个环境报了个js错误,TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [ ...

  5. C++ 面向对象的三个特点--多态性(二)

    运算符重载 运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型. 类外部的运算符重载 首先,我们通过一个例子来说明为什么要有运算符重载. // Complex.h cl ...

  6. 初识你---------Swift【下篇】

    Swift中的结构体 Swift的结构体对比OC来说,可以添加初始化方法.可以遵守代理协议等,同时:Swift的Bool类型的变量也是一个结构体,所以只能选择true和false. Swift中声明结 ...

  7. 复杂表格的树形结构的添加删除行div实现

    公司倒闭,换了工作,无奈选择了做外包这个差事,大公司进不去,小公司工资太低,可能也只能如此了.但无奈之举,亦不可浪费时间,多多磨练自己吧! 众所周知,做外包项目,其实就是做一些大公司的内部系统,多以管 ...

  8. 移动端H5---页面适配问题详谈(一)

    一.前言 昨天唠叨了哈没用的,今天说点有用的把.先说一下响应式布局吧,我一直认为响应式布局的分项目,一下布局简单得项目做响应式还是可以可以得.例如博客.后台管理系统等.但是有些会认为响应式很牛逼,尤其 ...

  9. C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串

    C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...

  10. C语言复习