题意:给你一些字符串,代表某个值被插入树中的位置。让你输出层序遍历。

题解:动态建树。

   由于输入复杂,将输入封装成read_input。注意输入函数返回的情况

   再将申请新节点封装成newnode().

   最后层序输出直接用bfs实现。

坑:我把ans.clear放到主程序的if里面,导致某特定情况无法初始化,wa了一页//以后debug真的别XJB改细节了上下语句顺序,一些无关紧要的处理,改之前想一想

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = ;
char s[maxn];
int failed;
struct node {
int v;
bool have_v;
node* leftson, *rightson;
node() :have_v(false), leftson(NULL), rightson(NULL) {}
}*root;
node* newnode() { return new node(); }
void addnode(int v, char *s) {
int n = strlen(s);
node * now = root; for (int i = ; i < n - ; i++) {
if (s[i] == 'L') { if (now->leftson == NULL) now->leftson = newnode();
now = now->leftson;
}
else {
if (now->rightson == NULL) now->rightson = newnode();
now = now->rightson;
} }
if (now->have_v) { failed = ; }
now->v = v;
now->have_v = ;
}
bool read_input() {
failed = false; root = newnode();
while () {
while (scanf("%s", &s) != )return false;
if (s[] == ')')break;
int v;
sscanf(&s[], "%d", &v);
addnode(v, strchr(s, ',') + );
}
return true;
}
list<int>ans;
void bfs() {
ans.clear();
queue<node> Q;
node now;
//int vis[maxn];
Q.push(*root);
while (!Q.empty()) {
now = Q.front(); if (!now.have_v) { failed = ; break; }
Q.pop();
if (now.leftson != NULL)Q.push(*now.leftson);
if (now.rightson != NULL)Q.push(*now.rightson);
ans.push_back(now.v);
} }
int main() {
while (read_input()) {
bfs();
if (failed)cout << "not complete" ;
else
{
cout << ans.front(); ans.pop_front();
for (auto t : ans)cout << ' ' << t;
}
cout << endl;
} system("pause");
}

【紫书】Trees on the level UVA - 122 动态建树及bfs的更多相关文章

  1. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  2. Trees on the level UVA - 122

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateo ...

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

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  4. 【紫书】Play on Words UVA - 10129 欧拉回路

    题意:给你1e5个字符串,若前一个的末尾字母等于当前的首字母,则可以连在一起(成语接龙一个意思)判断是否可以将他们连在一起 题解:将首位看作点,单词看作边.变成欧拉回路问题. 判断出入度是否相等,再用 ...

  5. 【紫书】 The Falling Leaves UVA - 699 递归得简单

    题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新le ...

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

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

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

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

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

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

  9. UVa 1339,紫书P73,词频

    题目链接:https://uva.onlinejudge.org/external/13/1339.pdf 紫书P73 解题报告: #include <stdio.h> #include ...

随机推荐

  1. Dubbo -- 系统学习 笔记 -- 示例 -- 只订阅

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 只订阅 问题 为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如 ...

  2. jumpserver安装及使用教程

    我自己是jumpserver的新手,以下两个链接是比较好的教程: 安装教程:http://blog.csdn.net/wanglei_storage/article/details/51001810 ...

  3. mysql5.7 服务无法启动的问题解决方法

    解决办法: 1.把MySQL文件低下的data文件删掉,如果没有的话,就不用管了: 2.在mysql安装路径下,执行mysqld --initialize命令进行初始化,mysql会自动帮你重新创建d ...

  4. MySQL,查看连接数和状态等

    1.MySQL> show status like '%connect%'; Connections,试图连接到(不管是否成功)MySQL服务器的连接数.   Max_used_connecti ...

  5. 更新npm至最新版本

    npm install npm@latest –g 或者@ 符号后面直接添加你想更新到的版本号

  6. 使用es6使数组的第一项和最后一项就行调换

    let arr = [1, 2, 3,4]; let a = arr.filter((item, index) => { return index > 0 && index ...

  7. 解决layui下拉选择框只能选择不能手动输入文字

    审查元素可以看到,layui的select下拉框是用input和div模拟出来的,所以,如下例子,我的解决方法是:$('.mySelect').find('input').removeAttr(&qu ...

  8. spring mvc 篇

    [1]spring mvc 实现多文件上传 http://blog.csdn.net/a1314517love/article/details/24183273 http://bbs.csdn.net ...

  9. c++多线程——锁技巧

    [转自]here 编写程序不容易,编写多线程的程序更不容易.相信编写过多线程的程序都应该有这样的一个痛苦过程,什么样的情况呢?朋友们应该看一下代码就明白了, void data_process() { ...

  10. linux下MySQL安装及设置(二)

    MySQL二进制分发包安装 去MySql官网下MySQL classic版mysql-5.6.30-osx10.11-x86_64.tar.gz  http://dev.mysql.com/downl ...