Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.

This problem involves building and traversing binary trees.

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.

Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.

Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string “not complete” should be printed.

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

题意

把给你的所有结点转成一棵树,并按层次遍历输出

题解

用链表的就不多说了,贴个数组的

代码

 #include<bits/stdc++.h>
using namespace std;
int Left[],Right[],V[],failed,cnt;
vector<int> vec;
void addnode(int v,char *s)
{
int n=strlen(s);//s[0]=','后的一个字符
int u=;
for(int i=;i<n;i++)
{
if(s[i]=='L')
{
if(Left[u]==-)
{
++cnt;
Left[cnt]=Right[cnt]=V[cnt]=-;
Left[u]=cnt;
}
u=Left[u];
}
else if(s[i]=='R')
{
if(Right[u]==-)
{
++cnt;
Left[cnt]=Right[cnt]=V[cnt]=-;
Right[u]=cnt;
}
u=Right[u];
}
else break;
}
if(V[u]!=-)failed=;//节点值已经存在
V[u]=v;
}
int bfs()
{
vec.clear();
queue<int> qu;
qu.push();
while(!qu.empty())
{
int top=qu.front();qu.pop();
if(V[top]==-)return ;//节点没有赋值
vec.push_back(V[top]);
if(Left[top]!=-)qu.push(Left[top]);
if(Right[top]!=-)qu.push(Right[top]);
}
return ;
}
int read()
{
int v;
char s[];
Left[]=Right[]=V[]=-;//0为根节点
failed=;
cnt=;
for(;;)
{
if(scanf("%s",s)!=)return ;
if(!strcmp(s,"()"))break;
sscanf(&s[],"%d",&v);//从s[1]开始读一个数字
addnode(v,strchr(s,',')+);
}
return ;
}
int main()
{
while(read())
{
if(failed==||bfs()==)
printf("not complete\n");
else
for(int i=;i<vec.size();i++)
printf("%d%c",vec[i],i==vec.size()-?'\n':' ');
}
return ;
}

UVa 122 Trees on the level(二叉树层序遍历)的更多相关文章

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

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

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

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

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

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

  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. 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

  9. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

随机推荐

  1. 解决eclipse新建项目看不到src/main/java目录办法

    1.eclipse->window->preferences->java->compiler->选择本地要用的Java版本 2.eclipse->window-&g ...

  2. webvtt字幕转srt字幕的python程序(附改名程序)

    最近写了两个比较简单的python程序,原有都是由于看公开课感觉比较费劲,一个是下载的视频无用的名字太长,另一个就是下载的vtt字幕播放器不识别,写了一个vtt转换成str字幕格式的文件 vtt to ...

  3. UML类图实例分析

    登录模块 某基于C/S的即时聊天系统登录模块功能描述如下: 用户通过登录界面(LoginForm)输入账号和密码,系统将输入的账号和密码与存储在数据库(User)表中的用户信息进行比较,验证用户输入是 ...

  4. sass实战演练01 - 外部文件引用和变量

    SASS是什么? 目前前端开发中css已经是公认的”前端程序员必须掌握”的知识,最早的css编写都是手工一条条写出来的,工作量大.不利于维护. 而sass的存在使得css开发可以像写代码一样最终生成一 ...

  5. Pandas时间序列

    Pandas时间序列 pandas 提供了一组标准的时间序列处理工具和数据算法 数据类型及操作 Python 标准库的 datetime datetime 模块中的 datetime. time. c ...

  6. for嵌套

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章

    <?phpuse Workerman\Worker;use Workerman\Lib\Timer; require_once '../../web/Workerman/Autoloader.p ...

  8. Java http请求工具类

    该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...

  9. mysql5.7.10开启慢查询

    MySql提供慢SQL日志的功能,能够记录下响应时间超过一定阈值的SQL查询,以便于我们定位糟糕的查询语句. 首先,查询当前mysql数据库是否开启了慢查询日志功能: show VARIABLES l ...

  10. U3D组件------CharacterController(角色控制器)

    角色控制器中有碰撞体和刚体的属性 Slope Limit:角色能爬的斜坡的坡度限制 Step Offset:角色走台阶的高度 Skin Width:当场景里面出现多个角色控制器的时候,两个对象在接触的 ...