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

题解:动态建树。

   由于输入复杂,将输入封装成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. 第四章 TCP粘包/拆包问题的解决之道---4.1---

    4.1 TCP粘包/拆包 TCP是一个“流”协议,所谓流,就是没有界限的一串数据.TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行包的划分,所以在业务上认为,一个完整的包可 ...

  2. javascript拖拽操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. ios开发之--所有设备的屏幕尺寸

    所有设备型号官网地址:https://www.theiphonewiki.com/wiki/Models iPhone: 机型 像素 比例 像素密度 屏幕尺寸 机型代码 发布日期 iPhone 2g ...

  4. Python 网络爬虫

    爬虫介绍 爬取图片 爬取文本 爬虫相关模块:re 爬虫相关模块:urllib 爬虫相关模块:urllib2 爬虫相关模块:cookielib 爬虫相关模块:requests 爬取需要登录的页面

  5. Django 创建第一个项目

    创建项目: [root@localhost ~]$ django-admin.py startproject web # web是项目名 [root@localhost ~]$ tree web/ w ...

  6. [SublimeText] Sublime Text 2 在 Ubuntu 上安装指南

    1. 下载Sublime Text 2 在官网下载对应系统位数的版本,从压缩包中提取出源代码,解压后文件夹中的"sublime_text"双击即可直接运行. 2. 建立快捷链接 将 ...

  7. Android源码中中一种常见的struct使用方法

    直接看例子: #include<iostream> #include<stdlib.h> using namespace std; struct Base{ int ba; i ...

  8. c++对象的生命周期

    C++ 的new 运算子和C 的malloc 函数都是为了配置内存,但前者比之后者的优点是,new 不但配置对象所需的内存空间时,同时会引发构造式的执行. 所谓构造式(constructor),就是对 ...

  9. c# linq update单个字段

    1.更新单个字段 /// <summary> /// 更新字段 /// </summary> /// <typeparam name="T">& ...

  10. #define #undef

    #include <stdio.h> int main( void ) { #define MAX 200 printf("MAX= %d\n",MAX); #unde ...