7-4 List Leaves

陈越、何钦铭-数据结构作业10:List Leaves层次遍历叶节点 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. 立即下载

7-4 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) 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

AC代码如下:

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

struct Node
{
    int data;
    char left;
    char right;
}T[20];
int n = 0;
int root = -1;
void build()
{
    char cl, ch;
    cin >> n;
    int i, j, k;
    int book [n];

    for (i = 0; i < n; i++)
    {
        book[i] = 0;
    }

    for (j = 0; j < n; j++)
    {
        cin >> cl >> ch;
        T[j].data = j;
        if (cl == '-')
        {
            T[j].left = -1;
        }
        else
        {
            T[j].left = cl - '0';
            book[cl-'0'] = 1;
        }
        if (ch == '-')
        {
            T[j].right = -1;
        }
        else
        {
            T[j].right = ch - '0';
            book[ch-'0'] = 1;
        }
    }
    for (k = 0; k < n; k++)
    {
        if (book[k] != 1)
        {
            root = k;
            break;
        }
    }
}

void traversal()
{
    queue <int> q;
    q.push(T[root].data);
    int arr[20];
    int i = 0;
    while (!q.empty())
    {
        int item = q.front();
        arr[i] = item;
        q.pop();
        if (T[item].left != -1)
        {
            q.push(T[item].left);
        }
        if (T[item].right != -1)
        {
            q.push(T[item].right);
        }
        i++;
    }
    bool flag = false;
    for (int j = 0; j < i; j++)
    {
        if (T[arr[j]].left == -1 && T[arr[j]].right == -1)
        {
            if(flag)
                cout << " ";
            cout << T[arr[j]].data;
            flag = true;
        }
    }
}
int main()
{
    build();
    traversal();
    return 0;
}

 

数据处理和分析之数据预处理:数据离散化 在某些情况下,根据业务知识或数据特性,我们可能需要自定义离散化的区间。例如,对于年龄特征,我们可能更关心儿童、青少年、成人和老年人这几个阶段,因此可以自定义这些区间。等宽离散化:将数据范围分成等宽的区间。等频离散化:将数据分成包含相同数量观测值的区间。聚类离散化:使用聚类算法(如K-means)来确定离散化区间。自定义区间离散化:根据领域知识或数据特性手动定义区间。# 自定义区间离散化示例# 假设我们根据业务理解,将年龄分为青年、中年和老年三个阶段。 阅读详情

相关推荐

Jetbrains IDE 配置上Gemini Code 插件,让你的的编程高效起飞~

Gemini Code Assist是 Google 家产品,基座模型为最新的Gemini 2.5 Pro。本篇实践了在 WebStrom 中Gemini Code Assist插件的高效编程。

2301_76993408的博客 4594

PTA_07_03_树2_list leaves(详细讲解)

PTA_07_03_树2_list leaves 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 gives a positive intege

sxycylq的博客 591

逆变器专题(3)-锁相环-PLL

锁相环作为并网逆变器的第一道门户,其锁相的精度将直接影响逆变器是否并网成功,若锁相精度较差,则会直接导致系统的功率因数下降,产生无功等等。在有关文献中指出,提高PLL带宽可以准确且快速进行锁相,而当电网电压含有谐波时,可适当降低带宽抵制谐波的干扰。成为变化量,而如果两者旋转速度达到一致时,所在的交轴分量。ki很大,即带宽很大时,稳定裕度不够,锁相环锁不住。为交流量,当频率锁定而相位没有锁定时,即。,即控制环前馈,可实现将锁相速度的加快。控为0,则可实现系统完美锁相。mod,取余,可减少存储空间。

cyc19980105的博客 6181

03-树2 List Leaves

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

Jingwang Li 364

从菜鸟到高手:我的Abaqus壳单元S4R、S3R、S8R选择心路历程

第一次打开Abaqus的单元库时,我被琳琅满目的壳单元类型晃花了眼。S4R、S3R、S8R、STRI65、S9R5...这些看似随意的字母数字组合,背后却隐藏着影响仿真精度的关键密码。作为一名从土木工程转行到汽车结构仿真的工程师,我花了三年时间才真正理解如何根据问题特性精准选择壳单元。这段学习历程充满试错与顿悟,今天就用几个关键案例,分享我的壳单元选择方法论。

weixin_42525637的博客 417

MIKE11实战:平原河网水动力建模从入门到精通(附GIS数据处理技巧)

本文系统介绍了MIKE11软件在平原河网水动力建模中的完整实战流程。从GIS数据预处理、河网构建、参数率定到结果分析,详细阐述了构建可靠数字孪生模型的关键步骤与技巧,旨在帮助水利工程师高效解决平原地区复杂水流模拟的挑战,为防洪决策提供科学依据。

weixin_28419039的博客 687

7-2 银行排队问题之单窗口“夹塞”版 (30分)

排队“夹塞”是引起大家强烈不满的行为,但是这种现象时常存在。在银行的单窗口排队问题中,假设银行只有1个窗口提供服务,所有顾客按到达时间排成一条长龙。当窗口空闲时,下一位顾客即去该窗口处理事务。此时如果已知第i位顾客与排在后面的第j位顾客是好朋友,并且愿意替朋友办理事务的话,那么第i位顾客的事务处理时间就是自己的事务加朋友的事务所耗时间的总和。在这种情况下,顾客的等待时间就可能被影响。假设所有人到达...

weixin_44415928的博客 1106

DeepSeek掘金——用DeepSeek模型构建AI 代理

在本综合指南中,我们将探讨如何利用 DeepSeek 的功能构建能够以有意义的方式理解、推理和交互的复杂 AI 代理。人工智能领域正在迅速发展,DeepSeek 的模型代表了智能 AI 代理开发的重大进步。在本综合指南中,我们将探讨如何利用 DeepSeek 的功能构建能够以有意义的方式理解、推理和交互的复杂 AI 代理。

3856

[浙大数据结构] 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

RK3568平台开发系列讲解(音频篇)配置声卡

📢本篇文章介绍如何配置声卡。

内核笔记 2448

Pytorch实现之SRGAN+CBAM的结构设计

在SRGAN的残差连接中加入了CBAM注意力机制,同时设计了四类损失来训练。

LJ1147517021的博客 569

PTA 03-树2 List Leaves 题目解析

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

zhuiyisinian的博客 608

YOLOv11改进 | 注意力篇 | YOLOv11引入24年最新的ELA注意力机制,并构建C2PSA_ELA

为了解决这些限制,本文介绍了一种高效的本地注意力(ELA)方法,实现了一个简单的结构,大幅度的性能改善。我们精心设计了ELA中的三个超参数,产生了四个不同的版本:ELA-T,ELAB,ELA-S和ELA-L,以满足不同视觉任务的具体要求,如图像分类,目标检测和语义分割。到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的,后期我会根据各种前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~

tsg6698的博客 1284

[PTA] 7-4 List Leaves

题目详情 题目分析: 我喜欢超短的代码!中间会有刻意压行的嫌疑从而影响可读性,还请谅解,我会给每一行标上作用,想浏览其他短代码可以看我其他博文 tree[i][0]表示第i个节点的左孩子,tree[i][1]表示第i个节点的左孩子 head表示队列头,rear表示队列尾 解释一下我如何找根节点的,我们看样例,没有出现的数一定是根节点,那怎么找到它呢?我们知道所有的节点的集合是个递增序列,先算出他们的和,在输入数据的时候出现一个就减掉一个,最后留下来的就是根节点 #include <cstdio&g

yym68686 320

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

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

7-4 List Leaves

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

lannister_awalys_pay的博客 397

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

7-4 List Leaves (25 分)

7-4 List Leaves (25 分) 这道题在逻辑上并不难,难在我们可能树不是很熟悉上。 我用了静态链表的方法构造树,用循环队列来用数组实现队列。 思路: 1、将数据读进来用,找到根节点。 2、递归找到所有的叶子节点。 3、用队列实现层次遍历,来找到树各层的元素。 4、比较叶子节点和层次遍历的结果得到最终的结果。 #include <stdio.h> #include <...

weixin_44720323的博客 658

温度PID控制PLC程序

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

PTA Data Structures and Algorithms 7-4 List Leaves

本题为陈越老师、何钦铭老师的数据结构课程的作业 7-4 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...

abc15766228491的博客 290

7-4-List Leaves-编程题

7-4-List Leaves-编程题解题代码测试结果问题整理 解题代码 #include<stdio.h> #include<stdlib.h> typedef struct STNode* pST; struct STNode { int left; int right; }; typedef struct queue* pqu; struct queue { s...

Aruasg的博客 313

PAT 7-4 List Leaves

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 number of no..

qq466546731的博客 189

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

cyberck的博客 475

浙大PAT遍历叶子结点:7-4 List Leaves

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

Yusamm的博客 272

Data Structures and Algorithms7-4——List Leaves

我的Data Structures and Algorithms代码仓:https://github.com/617076674/Data-Structures-and-Algorithms 原题链接:https://pintia.cn/problem-sets/16/problems/666 题目描述: 知识点:树的静态表示法、树的层序遍历 知识点:层序遍历树,寻找叶子节点 时间复...

清风阁 325

7-4 List Leaves (25分)**

corresponds 相当于 adjacent 相邻的 贴三份答案: #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 7, M = 5e4 + 7; vector<int> T[11]; void dfs(int u) { queue<int> q; q.push(u); bool f = 0; while(q.size() != 0) {

此地无银三百两 144
上一篇: 哈夫曼树的创建
下一篇: 7-3 树的同构
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值