【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

二叉树的话,直接用数组存就好了。
写个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的更多相关文章

  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——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

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

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

  5. UVa 122 Trees on the level

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

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

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

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

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

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

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

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

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

  10. 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. vue绑定内联样式

    v-bind:style 的对象语法十分直观--看着非常像 CSS ,其实它是一个 JavaScript 对象. CSS 属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case) ...

  2. js的类和继承

    因为我使用java语言入门的编程,所以对javascript的类和继承有种想当然一样,或者是差不多的感觉,但实际上两者还是有很多不同的 首先我们说类,javascript中类的实现是基于原型继承机制的 ...

  3. POJ 3042 区间DP(费用提前计算相关的DP)

    题意: 思路: f[i][j][1]表示从i到j的区间全都吃完了 现在在j点 变质期最小是多少 f[i][j][0]表示从i到j的区间全都吃完了 现在在i点 变质期最小是多少 f[i][j][0]=m ...

  4. deep-in-es6(五)

    解构 Destructuring: 解构赋值允许使用类似数组或对象字面量的语法将数组和对象的属性赋值给给中变量. 一般情况访问数组中的前三个元素: var first = arr[0]; var se ...

  5. git把本地文件上传到github上的步骤

    1.清除clean 2.返回上一级cd .. 3.克隆仓库地址git clone+地址 4.添加忽悠文件vim .gitignore 5查看cat .gitignore 6.进入到test,并且添加所 ...

  6. 41.C++多线程生产消费者模型

    #include <iostream> #include <thread> #include <mutex> #include <condition_vari ...

  7. JavaFx lineChart real-time Monitor

    JavaFx lineChart real-time Monitor   about memory public class EffectTest extends Application { Stac ...

  8. golang round

    func Round(f float64, n int) float64 {pow10_n := math.Pow10(n)return math.Trunc((f+0.5/pow10_n)*pow1 ...

  9. DG动态性能视图详解

    V$LOG 显示CONTROLFILE记录的LOG FILE信息. 列名          描述 GROUP#        日志组号 THREAD#       日志线程号 SEQUENCE#    ...

  10. 03002_MySQL数据库的安装和配置

    1.MySQL的安装 (1)下载mysql-5.5.49-win32.msi, 链接:MySQL安装包下载 密码:geqh : (2)打开下载的MySQL安装文件mysql-5.5.27-win32. ...