【例题 6-7 UVA - 122 】Trees on the level
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
二叉树的话,直接用数组存就好了。
写个bfs记录一下答案。
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 300;
string s;
int g[N+10][3],cnt;
vector <int> ans;
bool bfs(){
queue <int> dl;
dl.push(0);
while (!dl.empty()){
int x = dl.front();
dl.pop();
if (!g[x][2]) return false;
ans.push_back(g[x][2]);
for (int i = 0;i < 2;i++)
if (g[x][i])
dl.push(g[x][i]);
}
return true;
}
int main(){
// freopen("rush.txt","r",stdin);
while (cin >> s){
ans.clear();
memset(g,0,sizeof g);
cnt = 0;
bool ok = true;
while (!(s[0]=='(' && s[1]==')')){
int len = s.size();
bool root = false;
for (int i = 0;i < len;i++)
if (s[i]=='(' || s[i]==')' || s[i]==',') {
if (s[i]==',' && i+1<len && s[i+1]==')') root = true;
s[i]=' ';
}
stringstream ss(s);
int x;
ss >> x >> s;
if (root) s = "";
int now = 0;
for (int i = 0;i < (int) s.size();i++){
if (s[i]=='L'){
if (!g[now][0]) g[now][0] = ++cnt;
now = g[now][0];
}else{
if (!g[now][1]) g[now][1] = ++cnt;
now = g[now][1];
}
}
if (g[now][2]==0)
g[now][2] = x;
else{
ok = false;
}
cin >> s;
}
if (!ok || !bfs()){
puts("not complete");
}else{
for (int i = 0;i < (int) ans.size();i++){
printf("%d",ans[i]);
if (i==(int)ans.size()-1){
puts("");
}else putchar(' ');
}
}
}
return 0;
}
【例题 6-7 UVA - 122 】Trees on the level的更多相关文章
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- 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 ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- 70.nodejs操作mongodb
转自:https://www.cnblogs.com/whoamme/p/3467374.html 首先安装nodejs mongodb npm install mongodb var mongodb ...
- 玲珑学院 1014 Absolute Defeat
SAMPLE INPUT 3 2 2 2 1 1 5 1 4 1 2 3 4 5 4 10 3 1 2 3 4 SAMPLE OUTPUT 1 0 15 前缀和,每个元素都判断一下. #include ...
- 在物理 Data Guard 中对异构主备系统的支持 (文档 ID 1602437.1)
Data Guard中主数据库与物理备用数据库(Redo Apply)之间可以有什么差别?本说明针对重做应用和 Oracle Data Guard 12 发行版 1 进行了更新.它适用于 Oracle ...
- Vue中Mixins使用
mixins是一种分发Vue组件中可复用功能的一种灵活方式. mixins是一个JavaScript对象,可以包含组件中的任意选项,比如Vue实例中生命周期的各个钩子函数,也可以是data.compo ...
- docker的数据持久化
挂载宿主机的目录(实现很多容器共用一个宿主卷) [root@localhost ~]# docker run -itd --name web01 -v /var/www/html:/var/www/h ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- Android连接socket服务器上传下载多个文件
android连接socket服务器上传下载多个文件1.socket服务端SocketServer.java public class SocketServer { ;// 端口号,必须与客户端一致 ...
- beego的orm ,用的数据库sqlite3
测试 beego的orm ,用的数据库sqlite3 1 package main import ( "fmt" "github.com/astaxie/beego/or ...
- iTOP-4412开发板使用
使用环境:win7 旗舰64位,VMware11 使用使用板上提供的ubuntu12.04,用VMWARE直接打开虚拟机,因为之前开发epc9600开发板,所以虚拟机网络已经设置过,加载ubuntu1 ...
- SSO单点登录学习总结(3)—— 基于CAS实现单点登录实例
第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登录实例(我们可以布到多个机器上使用都是同一个道理的),一个服务器主机,和两个客户端虚拟主机 [html] view plaincop ...