Trees on the level 题解 二叉树的层序遍历 二叉树的删除 二叉树的结点插入

深入剖析:NAND Flash与SD NAND(贴片式SD卡)存储扇区分配表架构差异与优化策略 本文深入探讨了NAND Flash与SD NAND(贴片式SD卡)在存储技术中的架构差异及其优化策略。NAND Flash作为非易失性存储器,依赖主控芯片进行坏块管理和磨损均衡,而SD NAND则通过内置控制器和标准文件系统(如FAT)简化了存储管理。两者在写入性能、容量扩展和可靠性方面各有优劣,NAND Flash需通过磨损均衡和写入合并优化性能,而SD NAND则通过高容量和高速传输提升效率。文章为嵌入式系统开发者和硬件工程师提供了关于如何根据需求选择并优化存储介质的参考。 阅读详情

Trees on the level

题面

Background

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.

The Problem

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.

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

The 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

题目链接

HDU——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct node
{
    int val;
    node *lc;
    node *rc;
};

void delete_tree(node *p)
{
    if (p != NULL)
    {
        delete_tree(p->lc);
        delete_tree(p->rc);
    }
    delete p;
}

node *insert(node *p, int val, const char *point_str)
{
    if (p == 0)
        p = new node({0, 0, 0});
    switch (*point_str)
    {
    case ')':
        p->val = (p->val == 0) ? val : -1;
        break;
    case 'L':
        p->lc = insert(p->lc, val, point_str + 1);
        break;
    case 'R':
        p->rc = insert(p->rc, val, point_str + 1);
        break;
    }
    return p;
}

int main()
{
    node *root = NULL;
    string str;

    while (cin >> str)
    {
        const char *point_str = str.c_str(); // 读入的字符串上的指针
        // 如果第二个字符不是右括号,则继续输入
        if (point_str[1] != ')')
        {
            int val = 0;
            for (; isdigit(*++point_str); val = val * 10 + *point_str - '0')
                ;
            root = insert(root, val, ++point_str); // 插入该节点
            continue;
        }
        list<node *> level(1, root);
        stringstream res;
        // 层序遍历
        for (auto i = level.begin(); !level.empty(); i = (i == level.end() ? level.begin() : i))
        {
            node *cur_node = *i;
            i = level.erase(i);
            if (cur_node == 0)
                continue;
            if (cur_node->val <= 0) // 返回错误
            {
                level.clear();
                res.str("");
                break;
            }
            res << cur_node->val << ' ';
            i = level.insert(i, cur_node->lc);
            ++i;
            i = level.insert(i, cur_node->rc);
            ++i;
        }
        delete_tree(root); // 删除旧树
        root = 0;
        str = res.str();

        if (str.empty())
            str = "not complete";
        else
            str.erase(str.end() - 1);
        cout << str << endl;
    }
    return 0;
}
TC3xx系列LMU与SPR实战:将关键算法从Flash搬移至PSPR提升5倍执行效率 在实际电机控制项目中,通过将FOC算法全部迁移到PSPR,我们不仅将执行时间从56μs降至11μs,还将最坏情况下的时间偏差控制在±0.3μs以内。实测数据:在TC397上执行Flash中的32位浮点矩阵乘法,平均每条指令需要8.7个周期,而同样的代码在PSPR中仅需1.2个周期。传统开发中,工程师往往将所有代码放在Flash中运行,这相当于让F1赛车在乡村公路上行驶。迁移后不仅平均执行时间缩短,更重要的是时间抖动从±15%降低到±2%以内,这对闭环控制系统至关重要。不是所有代码都适合迁移到PSPR。 阅读详情

相关推荐

海康威视摄像机SDK二次开发--提取音频保存至文件

由于最近在开发海康威视摄像头,特此记录一下如何提取音频数据,这里主要依靠语音对讲返回的音频数据,通过回调函数写入文件中,加个WAV头即可播放,编码格式可以自己设置在代码中有注释 文件结构 其中CameraInfo内容可以参看海康威视摄像机SDK二次开发–实时预览视频流保存到指定文件中 HIKSDKSaveRealData中的内容 package ClientDemo; import ClientDemo.CameraInfo; import com.sun.jna.NativeLong; import

RichardoMu的博客 3173

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

因果推断综述-A Survey on Causal Inference

最近读到一篇讲述很全面的综述文献-《A Survey on Causal Inference》,对于接触因果推断不久的同学而言是特别详细的介绍和科普。文献很长,我会分成几部分介绍。 目录 摘要 第一部分:简介 第二部分:因果推断基础知识 第三部分:基于三种假设的因果推断方法 第四部分:实验 第五部分:应用 摘要 几十年来,因果推理是一个跨许多领域的关键研究主题,如统计、计算机科学、教育、公共政策和经济学。与随机对照试验相比,研究方向由于可用数据量大且预算要求低,如今,从观测数据..

海边凝望的博客 3182

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

TIA protal与SCL从入门到精通(1)——SCL编程入门

SCL语言是由Pascal而生的结构化程序语言,与梯形图相(LAD)比,SCL语言的灵活程度更高,易于编写控制算法。 本教程基于的平台如下 软件:TIA 15.1 CPU 1516 (1)直接创建纯SCL的程序块儿; (2)在LAD/FBD中在插入SCL程序段。 在程序段中直接右击插入即可,但是插入的程序段并不能完全使用SCL语法,GOTO等跳转指令就不能使用。在SCL中一般不能使用绝对地址进...

苦走一口田 7万+

第55篇:数据隐私保护技术盘点——差分隐私、同态加密与它们的坑(踩坑总结)

数据隐私保护是AI工程化中必须严肃对待的课题。差分隐私和同态加密是两把利器,但也都是“重武器”,使用不当反而会伤及自身。差分隐私像“模糊处理”,通过加噪来保证统计安全性,要小心管理隐私预算和效用权衡。同态加密像“保险箱里打算盘”,保证了计算的精确性,但必须承受巨大的性能和工程复杂度代价。我的建议是:从小规模实验开始,先用DP-SGD在MNIST/CIFAR-10等标准数据集上感受噪声与精度的关系;再用TenSEAL等易用的库尝试一个加密逻辑回归,体会密文膨胀和速度延迟。

ai_blue_pm的博客 281

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

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

zjsru_Beginner的博客 364

硬核反坑!YOLOv5训练避坑指南:根治梯度消失、不收敛、检测漏框三大核心问题(附实战代码+调优清单)

数据是基础,超参数是关键,模型是工具,训练策略是保障。梯度消失、不收敛、检测漏框这三大问题,本质上都是“数据-模型-训练”不匹配的结果。梯度消失/爆炸:优先调整学习率策略+梯度裁剪,其次优化模型结构;训练不收敛:优先清洗数据集,其次调优超参数,最后检查配置文件;检测漏框:优先重新聚类锚框,其次增强浅层特征,最后调整推理参数。YOLOv5训练的最高境界,不是调参,而是让数据、模型、训练策略三者完美匹配。按本文的方法反坑,你的YOLOv5训练一定能一次成功!🚀。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 233

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

huifeng_的博客 534

多智能体系统在军事仿真中的应用:AI人工智能战略推演

本文旨在全面解析多智能体系统在军事仿真领域的应用技术,重点探讨AI驱动的战略推演系统的设计原理和实现方法。研究范围涵盖从基础理论到实际应用的完整技术栈。本文采用从理论到实践的递进结构,首先介绍核心概念,然后深入算法实现,最后展示实际应用案例和未来展望。多智能体系统(MAS):由多个相互作用的智能体组成的分布式系统军事仿真(Military Simulation):通过计算机模拟军事行动和战争场景战略推演(Strategic Wargaming):通过模拟推演评估军事战略的有效性更高保真度的仿真。

AI天才研究院 1777

使用菱FX5U系列PLC实现以太网通信的具体方法示例

接下来,我们将创建一个简单的CPU通信功能,以实现与其他设备的以太网通信。在上述代码中,我们首先设置访问号,然后创建一个数据读取命令(M_READ)和一个数据写入命令(M_WRITE)。此外,在实际应用中,您还需要确保PLC和其他设备之间的网络连接正常,并且遵循相应的通信协议。在上述代码中,我们执行了数据读取命令,并将接收到的响应存储在M_RESPONSE变量中。通过简单CPU通信功能,菱FX5U系列PLC可以实现与其他设备的以太网通信。我们将提供相应的源代码和详细的说明,以帮助您轻松实现这一功能。

DevCyberX的博客 1525

Uva - 122 - Trees on the level

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

anpi3191的博客 178

STM32串口编程基础知识讲解

本篇文章给大家介绍一下串口的基础编程知识,这些知识是属于串口的理论知识,在开始学习编写代码的时候必须要掌握这些基本的知识点。本篇文章讲解了串口的基本概念知识,大家在学习编写代码时将其掌握才能更好的理解代码。

m0_49476241的博客 1208

5.2 二叉树

5.2 二叉树 比起紫书上二叉树的基础知识,黑书拓展了一些特殊的树形结构。 5.2.1 二叉树的存储 二叉树的基本概念就不再多做介绍,对一些用语不太明白的详见紫书6.3 树和二叉树。 关于二叉树的存储结构,一般用指针来实现: struct node{int value; node *l,*r;} l和r分别指向当前结点的左孩子和右孩子,当新建一个node时用new来申请内存,使用完毕时,用delete进行删除。 二叉树的遍历 主要就是两种遍历方式,宽度优先遍历和深度优先遍历。 宽度优先遍历 紫书中的遍

weixin_55835175的博客 382

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Tech Interview Handbook:树(Tree)编码面试专题——遍历、BST 性质与递归技巧全解

本文基于 [Tree 专题文档](https://link.gitcode.com/i/ef876c4b444387757ba842eb29b248f4) 展开,系统讲解二叉树与二叉搜索树(BST)的核心术语、三种遍历方式的递归与迭代写法、BST 的复杂度特性,以及面试中必须熟练掌握的递归技巧、分层遍历与常见边角用例。仓库内还附带了 Python / JavaScript 的遍历与树操作参考实现,

gitblog_00970的博客 192

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

如何在 Habitat-Lab 中快速搭建自定义任务环境:从零到跑通点导航的完整教程

Habitat-Lab 是 Meta 为具身AI训练开发的高层工具链,它在 Habitat-Sim 3D 仿真器之上封装任务定义,用一份 YAML 配置加十几行 Python 就能定义出你的自定义任务环境。本文面向具身AI方向的新手:以点导航为例子带你从零开始,读完你能装好可用的 3D 仿真环境、改通 habitat 任务配置、并用最小脚本观察智能体行为。 ## 先让 Habitat-Lab 跑

gitblog_00444的博客 466

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

UVa 122 - Trees on the level

题目:给你一颗二叉树上面的若干节点上的值(均为正数),判断从根到所有的给定的点的路径上的节点,             是不是都有值,且只被赋值一次。 分析:递归,搜索,树。             首先,利用递归建树,过程中记录是否有节点赋值两次;             然后,利用递归判断,查找是否有未被赋值的节点;             最后,利用bfs安深度输出每层的值。 说

小白菜又菜 3891

数据结构学习之-------树 Trees on the level HDU 1622

Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 330    Accepted Submission(s): 114 Problem Description Trees are fun

lxx_nico的专栏 790

122 - Trees on the level(动态分配空间解法)

Trees on the levelBackgroundTrees 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 oct

Switch的博客 3174

122:Trees on the level

Trees on the level哈哈 这次是一次AC,好开心啊version 1(指针链表版):#include&lt;bits/stdc++.h&gt; using namespace std; const int maxn = 256 + 5; struct node{ int num = -1; struct node* left = NULL; struct n...

Jingwang Li 238

UVa122-Trees on the level

UVa122-Trees on the level 1.题目描述:点击链接 2.问题分析: 简单地来说,就是输入一组字符串,表示二叉树上某些节点的值和位置,这些节点不一定可以组成一颗完整的二叉树,可能出现缺少某个节点或者某个节点冗余的情况。 需要我们进行判断是否能组成一颗完整的二叉树,若可以按照由上到下,由左到右的顺序输出每个节点的值,若不能...

我来人间打个滚 190

UVA 122 - Trees on the level

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

188

[UVA122]Trees on the level

以任意顺序给出一棵二叉树的节点,格式如: (5,LLRL), 数字表示该节点的值. 要求输出一棵树中从一个叶子走到根节点的取到最小权值和的那个叶子节点,假如有多个叶子节点能取到最小的权值,则输出叶子权值本身最小的那个叶子.题目保证了每个节点的权值都是不同的.假如一组输入,存在一个节点被赋值超过1次或者没被赋值,则输出"not complete".每组输入用()作为该组数据的终结符 题目其实没啥

Long days journey into the night 480

树的层次遍历,Trees on the level, UVA 122

题目链接:https://vjudge.net/problem/UVA-122 题解:题意就是按题目要求建立二叉树,然后进行BFS,我在uDebug上进行了测试,都通过了,但是仍然没法通过vjudge上的测试例,找不到问题在哪儿?希望有大神能帮助看看,多谢,多谢。 代码:(注意没有AC) #include&lt;iostream&gt; #include&lt;cstdio&gt; #in...

PAN_Andy的博客 324
上一篇: 洛谷 P4124 [CQOI2016] 手机号码 题解 数位dp
下一篇: Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers 题解 数位dp
bbc_ouo
博客等级 码龄3年 2096粉丝 132原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值