03-树2 List Leaves(25 分)

03-树2 List Leaves (25分) 03-树2 List Leaves (25分) 思路分析:本体要自己创建一个二叉树,然后按照层序输出叶节点,一说到层序我们就会想到层序遍历,层序遍历我们常用的方法无非是队列,从根节点开始进入队列,设置一个临时变量,判断队列是否为空,然后获取队列头元素,然后出栈,并且输入它,判断这个临时变量是否有左右孩子如果有,则依次将左孩子,右孩子进入队列,依次类推反复执行。 这道题要用到queue的库函数,就用C++来实现的,queue容器提供了以下的操作: front():返回 queue 中第一个元素的引用。如果 阅读详情

题目来源:中国大学MOOC-陈越、何钦铭-数据结构-2018春
作者: 陈越
单位: 浙江大学

问题描述:
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5

解答:
目标是找出所有叶子节点,按序输出。(要求从上到下,从左到右顺序,可以用BFS做)
一开始看到题目有点懵,感觉重要的是找到树根,其他就简单了,BFS都是套路。树根怎么找呢?可以用一个0到N-1数组表示,注意输入为字符类型,若要数值型数据要减‘0‘以输入左节点为例:

        char temp;
        cin>>temp;
        if(temp!='-')
        {
            tree[i].leftChild=temp-'0';
            isHead[temp-'0']=false;
        }

找到根节点后利用队列做BFS即可

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

struct node
{
    int leftChild;
    int rightChild;
    node()
    {
        leftChild=-1;
        rightChild=-1;
    }
};
node tree[10];
bool isHead[10];
int main()
{
    int M;
    cin>>M;
    fill(isHead,isHead+10,true);
    for(int i=0;i<M;i++)
    {
        char temp;
        cin>>temp;
        if(temp!='-')
        {
            tree[i].leftChild=temp-'0';
            isHead[temp-'0']=false;
        }
        cin>>temp;
        if(temp!='-')
        {
            tree[i].rightChild=temp-'0';
            isHead[temp-'0']=false;
        }
    }
    int head;
    for(int i=0;i<M;i++)
    {
        if(isHead[i]==true)
        {
            head=i;
            break;
        }
    }
    queue<int> bfs;
    queue<int> result;
    bfs.push(head);
    while(!bfs.empty())
    {
        int frontNode=bfs.front();
        bfs.pop();
        if(tree[frontNode].leftChild==-1&&tree[frontNode].rightChild==-1)
            result.push(frontNode);
        if(tree[frontNode].leftChild!=-1)
            bfs.push(tree[frontNode].leftChild);
        if(tree[frontNode].rightChild!=-1)
            bfs.push(tree[frontNode].rightChild);
    }
    cout<<result.front();
    result.pop();
    while(!result.empty())
    {
        cout<<" "<<result.front();
        result.pop();
    }
    return 0;
}
03-树1. List Leaves.zip 遍历树。 主要是通过队列来实现的。 是C++的源代码,工程文件。 立即下载

相关推荐

Apollo MPC横纵向耦合控制学习笔记

Apollo MPC横纵向控制原理学习笔记

weixin_39199083的博客 1万+

03-树2 List Leaves (25分) (查找叶子节点)

03-树2List Leaves(25分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each ca...

Kunaly 1401

MATLAB实战:蓝牙GFSK调制解调全流程解析(附误码率优化技巧)

本文深入解析了蓝牙GFSK调制解调在MATLAB中的全流程实现,包括高斯滤波器设计、调制解调技术及误码率优化技巧。通过实战案例和代码示例,帮助工程师提升蓝牙通信系统的性能表现,特别是在降低误码率方面的有效方法。

weixin_29046035的博客 277

浙大数据结构03-树2 List Leaves (25分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a po...

leslie___的博客 509

双有源桥变换器(DAB)TPS控制:从理论建模到Simulink/PSIM仿真实践

本文深入探讨了双有源桥变换器(DAB)的三重移相(TPS)控制技术,从理论建模到仿真实现。文章详细分析了TPS相比SPS和DPS的优势,阐述了其12种工况的划分逻辑与电流应力最优调制策略,并提供了在Simulink和PSIM平台搭建仿真模型的详细步骤与实战经验,帮助工程师掌握这一高效控制方法。

white的博客 584

自然语言处理在智能客服系统中的应用

引言与背景 1.1 书籍的目的和内容概述 《自然语言处理在智能客服系统中的应用》旨在探讨自然语言处理(NLP)技术在智能客服系统中的应用,帮助读者理解NLP的核心概念及其在解决实际业务问题中的关键作用。随着人工智能技术的快速发展,自然语言处理成为构建智能客服系统的核心模块之一。本书将深入解析NLP

AI天才研究院 968

数据结构与算法——03-树2 List Leaves(浙大陈姥姥《数据结构与算法》)

1. 题目描述 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integerN(≤10) which is the total num..

jay_zzs的博客 388

C# 工业视觉性能极致优化:多线程推理池 + 对象复用 + 批量检测,速度提升 10 倍实战

set;} // 输入图像set;} // 异步返回结果set;} // 用于监控延迟性能优化不是魔法,而是对资源生命周期的精细管理和对硬件特性的充分利用。多线程池解决了 CPU/GPU 闲置问题。对象池解决了 GC 停顿问题。零拷贝解决了总线带宽瓶颈。严格的 Dispose解决了内存泄漏问题。这套架构已在多个高速锂电、3C 电子检测产线落地,轻松应对 200ms 甚至 50ms 的严苛节拍。记住,在工业领域,稳定性与速度同等重要。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 304

浙大数据结构习题笔记:03-树2 List Leaves (25分)

03-树2 List Leaves (25分) 原题如下: Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) whi

Jimobbb的博客 593

手把手教你学Simulink——机器人控制场景实例:基于Simulink的双足人形机器人步态生成与ZMP稳定控制仿真

ZMP是地面上合力矩在水平方向为零的点。对于平面行走: [ x_{\text{zmp}} = x_c - \frac{z_c}{g} \ddot{x}_c ]📌稳定性判据ZMP必须始终位于支撑脚的凸包(即脚掌范围内)本文完成了基于Simulink的双足人形机器人ZMP稳定控制仿真,实现了:✅ 基于LIPM生成稳定CoM轨迹✅ 构建ZMP参考与验证机制✅ 设计简化踝关节PD控制器✅ 验证ZMP始终位于支撑脚内 → 动态平衡✅ 为人形机器人步态规划奠定理论基础。

MHD0815的博客 139

手把手教你学Simulink——基于Simulink的储能PCS(功率转换系统)离网V/f控制仿真示例

本文带你从零开始,在Simulink中硬核还原了一套完整的100kW 储能PCS离网V/f控制系统。通过引入:✅无PLL的自主角度生成机制(实现真正的“构网”能力)✅电压外环与电流内环的双闭环解耦架构(兼顾稳态精度与动态响应)✅黑启动软充电与有源阻尼等工程化手段(确保系统平滑建压与抗谐振)核心收获透彻理解了储能系统在无大电网支撑下的自主运行机理。掌握了从“跟网(Grid-Following)”向“构网(Grid-Forming)”跨越的控制算法核心差异。

MHD0815的博客 77

[浙大数据结构] 03-树2 List Leaves (25分)

1 题目描述 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line ...

努力变得不菜的菜鸡的博客 1401

Windows 11 安装 uv:手动下载、配置国内源

本教程详细介绍了在 Windows 11 系统下安装并配置 Python 包管理器 uv 的完整流程,包括配置环境变量 PATH、设置清华 PyPI 镜像源,以及创建收据文件以支持未来更新。适用于因网络限制无法通过官方脚本自动安装的用户,操作步骤清晰,配有图文指引,帮助读者高效完成本地部署。

寒水馨 · 轻量级技术文档 · hanshuixin.org 7610

03-树2 List Leaves (25分)

03-树2 List Leaves (25分)Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case,

david_liang 2507

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

03-树2 List Leaves (25 分

03-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, ...

weixin_38732825的博客 170

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

03-树2 List Leaves(25 分)

03-树2 List Leaves(25 分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, t...

中央单元 586

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

03-树2 List Leaves (25 分)

03-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. 输入格式: Each input file contains one test case. For each case, the first line ...

CO₂的博客 411

03-树2 List Leaves (25 分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a ...

听维也纳的海风 233
上一篇: 03-树1 树的同构(25 分)
下一篇: 03-树3 Tree Traversals Again(25 分)
peterchen96
博客等级 码龄10年 6粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值