Trees on the level

【代码超详解】UVA 122 Trees on the Level(二叉树、BFS) 紫书 第六章 例题 6-7 Trees on the level, Duke 1993, UVa 122 第一回手写二叉树,今天就为这道题磕了一整天才过了样例,不过好在一提交就AC了。 一、题目描述 输入包含多组数据。每组数据给出一系列有序对 (v, s) (单个空格隔开两个对)代表二叉树的某个节点的值,你的任务是从根到叶输出各个节点的值 v 。同一层输出顺序自然是从左到右。最多有 256 个节点... 阅读详情

题目:

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. 

This problem involves building and traversing binary trees. 
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes. 

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1. 

For example, a level order traversal of the tree 

 
is: 5, 4, 8, 11, 13, 4, 7, 2, 1. 

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once. 
 

Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses. 

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file. 
 

Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printed

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

 

题意:

输入一颗二叉树,你的任务是按照从上到下,从左到右的顺序输出各个结点的值。每个结点都按照从根结点到他的移动顺序给出(L表示左,R表示右)。在输入中,每个结点的左括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括号()结束。如果不能建成树输出 not complete;

 

思路:

建一棵树,然后把每个节点的路径保存到set容器中,保证路径不能相同,之后利用深搜,从字符串为空,每次添加L或R检查新的字符串是否存在,如果存在sum+1,如果最后sum == t(输入的节点数)则说明能构成树。

 

代码:

#include<stdio.h>
#include<set>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int t,fff,sum;
set<string>h;
struct node
{
    int a,l;
    char ss[300];
} s[300];
bool cmp(node x,node y)
{
    if(x.l==y.l)return strcmp(x.ss,y.ss)<0;
    else return x.l<y.l;
}
void dfs(int a,string as)
{
    int i=a;
    while(i<t)
    {
        if(as.length()+1<strlen(s[i].ss))break;
        string ac=as+'L';
        if(ac.compare(s[i].ss)==0)
        {
            sum++;
            dfs(i+1,ac);
        }
        ac=as+'R';
        if(ac.compare(s[i].ss)==0)
        {
            sum++;
            dfs(i+1,ac);
            break;
        }
        i++;
    }
}
int main()
{
    char k[300];
    while(~scanf("%s",k))
    {
        h.clear();
        t=0;
        fff=0;
        int flag=0;
        int gen=-0x3f3f3f3f;
        if(strlen(k)==2)
        {
            printf("not complete\n");
            continue;
        }
        else do
            {
                int l=strlen(k);
                if(l==2)
                    break;
                int num=0;
                int i=1,f=0,bl=0;
                char b[300];
                for(int j=0; j<300; j++)b[j]='\0';
                while(k[i]<='9'&&k[i]>='0')
                {
                    num=num*10+k[i]-'0';
                    i++;
                }
                i++;
                while(k[i]=='L'||k[i]=='R')
                {
                    f=1;
                    b[bl++]=k[i];
                    i++;
                }
                if(f==0)
                {
                    if(flag==0)
                    {
                        gen=num;
                        flag=1;
                    }
                    else fff=1;
                }
                else
                {
                    if(h.find(b)!=h.end())
                        fff=1;
                    else
                    {
                        h.insert(b);
                        s[t].a=num;
                        strcpy(s[t].ss,b);
                        s[t++].l=strlen(b);
                    }
                }
            }
            while(scanf("%s",k));
        sort(s,s+t,cmp);
        if(gen==-0x3f3f3f3f)fff=1;
        if(fff==1)printf("not complete\n");
        else
        {
            sum=0;
            dfs(0,"");
            if(sum!=t)fff=1;
            if(fff==1)printf("not complete\n");
            else
            {
                printf("%d",gen);
                for(int i=0; i<t; i++)
                    printf(" %d",s[i].a);
                printf("\n");
            }
        }
    }
    return 0;
}

 

『杭电1622』Trees on the level Problem Description Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graph 阅读详情

相关推荐

在Unity中为Sentis部署的YOLOv8模型集成NMS层并实现移动端性能优化

本文详细讲解了在Unity中利用Sentis部署YOLOv8模型时,如何通过代码动态集成NMS层以解决移动端性能瓶颈。文章核心在于避免导出时绑定NMS,转而使用Sentis原生API在运行时高效构建NMS处理流程,并分享了关键的GPU数据操作与移动端性能优化技巧,确保模型在移动设备上流畅运行。

p1q2r的博客 1093

Trees on the level (二叉搜索树的还原)

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to...

自渡的博客 714

从收音机到Arduino:手把手教你用BJT三极管搭建一个简易音频放大器(附电路图与元件清单)

本文详细介绍了如何使用BJT三极管搭建简易音频放大器,涵盖电路原理、元件选择、设计步骤及调试技巧。通过实践项目帮助电子爱好者深入理解BJT的放大特性,并提供与Arduino结合的扩展应用方案,适合创客和DIY玩家。

aitie1479的博客 437

UVA-122-TREE ON THE LEVEL

UVA-122-TREE ON THE LEVELsubjectTrees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and

丹朗的博客 416

PyQt5 + HFSS:给你的仿真脚本做个专属GUI界面(零基础搭建指南)

本文详细介绍了如何使用PyQt5为HFSS仿真脚本创建专属GUI界面,适合零基础用户快速上手。通过Python二次开发,实现参数可视化调整、进度实时监控等功能,大幅提升团队协作效率。文章涵盖环境配置、界面设计、HFSS集成到打包部署的全流程,是仿真工程师提升工作效率的实用指南。

weixin_42535720的博客 185

Django 小实例S1 简易学生选课管理系统 —— 〇、初步介绍与演示

python Django实现的一个简易的教务选课系统。 介绍与演示的视频版本已发到我的b站: https://www.bilibili.com/video/BV1er4y1w7ty。 项目已上传到我的github: https://github.com/BigShuang/SimpleStudentCourseManagementSystem。 S1总目录: 〇、初步介绍与演示 一、项目流程梳理与数据库设计 ======================= 大爽歌作,made by big shuang.

大爽老师的博客 5944

A - Trees on the level(树的层序遍历)

主要使用了c++STL中的set。用排序的方法对输入进行排序,根据字符的长度以及相同长度的字典序从小到大,正好对应树从上到下。1.某些节点无法从根节点出发到达。2.开始处理数据时都需要清空路径。...

zjsru_Beginner的博客 364

STM32F103C8T6+I2C+DMA读取陀螺仪模块JY901S(硬件I2C

说明:1.本实验代码适用移植一切stm32f1系列;2.本实验使用的是外设资源硬件I2C2;3.本实验运用的是DMA单次模式,如果需要DMA的循环模式,请移步我的另一篇文章;4.本文章不提供陀螺仪校准教学,并是在没有校准陀螺仪情况下读取数据;5.本文章是作者的学习记录及心得,如有不足,欢迎指正。 总结:亲测stm32f103c8t6最小系统板通过片上外设资源I2C配合DMA读去陀螺仪模块jy901s偏航角有效。

anquanyuanqqadsy的博客 6171

Uva - 122 - Trees on the level

难点在于二叉树的生成和宽度优先遍历BFS,最后要注意防止内存泄漏,虽然题目没有内存限制,但是优化考虑还是做一下释放内存,因为这个不是效率型的问题,测试时间短到没有无法显示。 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <...

anpi3191的博客 178

正点原子RK3588 Android 12镜像构建实战:从环境配置到烧录验证全指南

本文提供了一份基于正点原子RK3588开发板的Android 12镜像构建全流程实战指南。内容涵盖从Ubuntu环境配置、SDK获取、编译内核与系统,到最终镜像打包与烧录验证的完整步骤,并分享了内核驱动定制与系统优化技巧,旨在帮助开发者高效完成瑞芯微RK3588平台的系统定制工作。

grass的博客 810

[运营] ext4分区遇到“Structure needs cleaning”问题,怎么解决 - 结构需要清理

摘要:当ext4文件系统出现"Structure needs cleaning"错误时,表明存在元数据损坏。解决方法包括:1)检查系统日志确认错误;2)卸载受影响分区;3)使用fsck.ext4分步骤修复(先测试模式-n,再保守修复-p,严重损坏时用-y强制修复);4)检查修复结果;5)重新挂载。若数据丢失可尝试从lost+found恢复。建议定期备份、避免异常关机并定期检查文件系统,以防硬件故障导致类似问题。修复前务必备份重要数据。

Rudon滨海渔村的博客 1358

uva122 trees on the level

看不下去,找点简单的,再回来写

Dosth_Magic 625

Kriging克里金模型实现数据回归预测Matlab代码

Kriging克里金模型实现数据回归预测Matlab代码

天天酷科研的博客 603

(二叉树建树 先序遍历)UVA_122 Trees on the level

UVA_122 Trees on the level题意让我们构建一棵树,给出每一个节点是从根节点怎么走才能走到. 比如(11,LL)就是根节点的左子树的左子节点的位置权值为11.然后让给出这棵树的先序遍历结果. 如果出现下列情况,输出”not complete” 重复赋值 没有根节点 有节点没有被赋值 给出超过一个节点 解决这个题的输入比较坑啊,刚上来感觉解决不了…

ZhangYang 410

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

UVA-122(Trees on the level)

Trees on the level 题目链接: https://vjudge.net/problem/UVA-122 题目意思: 给你一些(,)让你建立一棵树,直到输入()结束建树,然后判断树是否完整,如果没有结点未赋值或者被赋值两次,就按层次遍历输出树,否则输出not complete 代码: #include <algorithm> #include <m...

weixin_30278237的博客 90

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

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

Pengwill's Blog 361

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Trees on the level UVA - 122

Trees on the level UVA - 122 紫书6.3.2 二叉树的层次遍历 采用动态结构创建二叉树 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace s...

NaNa 154

[UVA] Trees on the level

题目:https://vjudge.net/problem/UVA-122 #include<cstdio> #include<cstring> #include<vector> #include<queue> #pragma warning(disable:4996) using namespace std; const int maxn =...

weixin_37992828的博客 163

122 Trees on the level

一个二叉树的题; 看的书上和网上的结合,用一个动态结构构造一棵树,比较麻烦,但好理解 #include #include #include using namespace std; const int maxn=260; int ans[maxn];//用来装输出的数的结点值 struct Node { bool have_value;//是否被定义 int v;//结

DS00HY的小博客 274

122 - Trees on the level

Background Trees arefundamental in many branches of computer science. Current state-of-the artparallel computers such as Thinking Machines' CM-5 are based on fattrees. Quad- and octal-trees are funda

gaoxiangnumber1——Gao Xiang's Blog 483
上一篇: 二叉搜索树
下一篇: The order of a Tree
admin__
博客等级 码龄8年 3粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值