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

【题意】

在这里输入题意

【题解】

二叉树的话,直接用数组存就好了。
写个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. js---10时间类

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. C#制作文本转换为声音的demo,保存音频文件到本地

    TTS(Text To Speech)可以实现把文本转换成语音并朗读出来.Windows Xp可以使用Com组件--Microsoft Speech Object Library实现TTS,Windo ...

  3. 8.ZOrder

    T3LayerZorder.h #pragma once #include "cocos2d.h" USING_NS_CC; class T3LayerZorder:public ...

  4. 竖向 两级手风琴 TAB 栏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Mysql学习总结(17)——MySQL数据库表设计优化

    1.选择优化的数据类型 MySQL支持很多种不同的数据类型,并且选择正确的数据类型对于获得高性能至关重要.不管选择何种类型,下面的简单原则都会有助于做出更好的选择: (1).更小通常更好 一般来说,要 ...

  6. ReactNavtive框架教程(4)

    开头的响应码, 这些代码都很实用. 比如202 和 200表示返回一个推荐位置的列表.当完毕这个实例后.你能够尝试处理这些返回码.并将列表提供给用户选择. 保存,返回模拟器,按下Cmd+R ,然后搜索 ...

  7. Css3 过渡(Transition)特效回调函数

    Css3 出来之后,能够说是替代了Flash,通过使用Html5和Css3的完美结合.就能够做出不论什么你想得到的特效,这里不再阐述... 近期在做一个喝水签到的功能.在想签到成功之后,签到框能够模拟 ...

  8. mysql 批量删除数据

    批量删除2000w数据 使用delete from table太慢 //DELIMITER DROP PROCEDURE if EXISTS deleteManyTable; create PROCE ...

  9. 读阮一峰《ECMAScript 6 入门》小结

    读阮一峰<ECMAScript 6 入门>小结,http://es6.ruanyifeng.com/ 1. ES6简介 Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转 ...

  10. Loadrunner经典测试实例

    Loadrunner经典测试实例