题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点。

分析:先建树,建树的过程中,沿途结点都申请了内存,所以在bfs的时候如果该结点有内存,但是未赋值,那就算not complete,别忘了释放内存,虽然这不影响AC,但是注意内存泄漏是好习惯。

PS:

strchr函数原型:char * strchr(char * str, int ch); 功能就是找出在字符串str中第一次出项字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。
strstr 函数原型: char * strstr(char * str1,char * str2);功能就是找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2),找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[MAXN];
bool ok;
vector<int> a;
struct Node{
Node* left;
Node* right;
int value;
bool flag;//结点是否被赋值过
Node():left(NULL), right(NULL), flag(false){}
};
Node* root;
void addNode(int v, char* s){
Node *u = root;
int len = strlen(s) - ;
for(int i = ; i < len; ++i){
if(s[i] == 'L'){
if(u -> left == NULL){
u -> left = new Node();
}
u = u -> left;
}
else{
if(u -> right == NULL){
u -> right = new Node();
}
u = u -> right;
}
}
if(u -> flag == true){
ok = false;//某结点超过一次被赋值
}
else{
u -> flag = true;
u -> value = v;
}
}
void bfs(){
queue<Node*> q;//因为用指针遍历的,所以加*
if(root != NULL){
if(root -> flag){
q.push(root);
}
else{
ok = false;
return;
}
}
while(!q.empty()){
Node *tmp = q.front();
a.push_back(tmp -> value);
q.pop();
if(tmp -> left != NULL){
if(tmp -> left -> flag)
q.push(tmp -> left);
else{
ok = false;
return;
}
}
if(tmp -> right != NULL){
if(tmp -> right -> flag)
q.push(tmp -> right);
else{
ok = false;
return;
}
}
}
}
void delete_tree(Node *root){
if(root == NULL) return;
if(root -> left != NULL) delete_tree(root -> left);
if(root -> right != NULL) delete_tree(root -> right);
delete root;
}
int main(){
while(scanf("%s", s) == ){
ok = true;
a.clear();
if(!strcmp(s, "()")){
printf("not complete\n");
return ;
}
int v;
sscanf(s + , "%d", &v);//第一个参数必须是该整数在此字符串中第一次出现的位置,v中存的是s中第一个出现的整数
delete_tree(root);
root = new Node();
addNode(v, strchr(s, ',') + );//返回字符','在字符串s中的地址的位置,找不到则返回null
while(scanf("%s", s) == ){
if(!strcmp(s, "()")) break;
sscanf(s + , "%d", &v);
addNode(v, strchr(s, ',') + );
}
bfs();
if(!ok) printf("not complete\n");
else{
int len = a.size();
for(int i = ; i < len; ++i){
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
}
return ;
}

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(二叉树层序遍历)

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

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

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

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

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

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

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

  6. 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 ...

  7. UVa 122 Trees on the level

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

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

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

  9. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 正确使用 Android 的 Theme 和 Style

    原文:http://www.tuicool.com/articles/ZjEZFj Android 5.0 可以给一个 View 单独设置一个 theme 了,其主要用途就是用在 ToolBar 上, ...

  2. Linux centosVMware iptables规则备份和恢复、firewalld的9个zone、firewalld关于zone的操作、firewalld关于service的操作

    一.iptables规则备份和恢复 保存和备份iptables规则 service iptables save //会把规则保存到 /etc/sysconfig/iptables 把iptables规 ...

  3. RIFF

    RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构. RIFF文件所包含的数据类型由该文件的扩展 ...

  4. Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全

    通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...

  5. HTML中元素 标签 属性

    HTNL中元素是以开始标签开始 结束标签结尾的 如:<p>this is a paragraph </p> <p>是开始标签   </p>是结束标签  ...

  6. express框架安装及中间件原理

    本文主要介绍express中间件的原理,来应对面试. 1.安装express及初始化: npm install express-generator -g   =>   express expre ...

  7. 如何在adapter 中调用activity的方法

    如何在adapter 中调用activity的方法 2015-08-07 17:06匿名 | 浏览 808 次  iWorkjavaAndroid public class HistoryData e ...

  8. detectron2 配置记录

    1. RuntimeError: Not compiled with GPU support (ROIAlign_forward at /home/oliver/PycharmProjects/det ...

  9. 九:File类,文件的操作

    File的常用方法:

  10. gitlab clone或者pull 仓库

    今天在学git操作,想从gitlab上面clone下来并操作一下,但是一直出现 没有权限的错误,一直搞不定 后来才知道,需要ssh密钥才可以 ssh-keygen -t rsa -C "ex ...