浙大数据结构课后习题 练习三 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 (≤) 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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
/**树结构体*/
struct tn{
int index;
string lc;
string rc;
};
/**层序遍历,返回一个vec*/
vector<int> levelOrderTreserve(tn tree[],int index){
vector<int> res;queue<tn> que;tn tmp;
que.push(tree[index]);
while(!que.empty()){
tmp=que.front();
que.pop();
if(tmp.lc!="-") que.push(tree[(tmp.lc[]-'')]);
if(tmp.rc!="-") que.push(tree[(tmp.rc[]-'')]);
if(tmp.lc=="-"&&tmp.rc=="-") res.push_back(tmp.index);
}
return res;
}
int main()
{
/**input*/
int T;
cin>>T;
tn tree[T];int judgeRoot[T];int rootPos;
for(int i=;i<T;i++) judgeRoot[i]=;
for(int i=;i<T;i++){
cin>>tree[i].lc>>tree[i].rc;tree[i].index=i;
/**找出根节点*/
if(tree[i].lc!="-") judgeRoot[tree[i].lc[]-'']++;
if(tree[i].rc!="-") judgeRoot[tree[i].rc[]-'']++;
}
for(int i=;i<T;i++) if(!judgeRoot[i]) rootPos=i;
vector<int> res=levelOrderTreserve(tree,rootPos);
for(int i=;i<res.size();i++) {
cout<<res[i];
if(i!=res.size()-) cout<<" ";
}
system("pause");
return ;
}
浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)的更多相关文章
- 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 浙大数据结构课后习题 练习二 7-3 Pop Sequence (25 分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)
PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称P ...
- PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)
PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分) 每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...
- PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)
PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分) 在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...
- PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)
PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...
- JAVA语言程序设计课后习题----第三单元解析(仅供参考)
1 本题水题,记住要知道输入格式即可 import java.util.Scanner; public class test { public static void main(String[] ar ...
随机推荐
- Python 自动化
一.Win32 GUI自动化测试模块: 1. pywinauto: 下载链接:http://sourceforge.net/projects/pywinauto/ 在线文档:http://pywina ...
- lab 颜色模式的生理原因 黄色, 洋红色 刺眼。 绿色,蓝色,不刺眼。
hsb 颜色模式理解了. lab 颜色模式,都说是生理原因.没说是啥生理原因. 猜测:黄色, 洋红色 刺眼. 绿色,蓝色,不刺眼. https://blog.csdn.net/self_mind/ ...
- [笔记] 如何在Windows上同时打开多个钉钉?
钉钉防多开原理 常规程序防止多开,会使用Mutex. 钉钉是常规程序,所以也是使用Mutex. 查找钉钉使用的Mutex 工具:ProcessExplorer.exe 启动钉钉,然后使用Process ...
- 【转】hbase meta表修复
[From]https://www.iteye.com/blog/blackproof-2052898 meta表修复一 查看hbasemeta情况 hbase hbck .重新修复hbase met ...
- Aes加密/解密示例项目
#AesEncrypt:Aes加密/解密示例项目 <br> 附件中的“AesEncrypt.zip”是本项目的exe文件,可直接下载下来运行和查看. *高级加密标准(英语:Advanced ...
- 前端vscode常用插件
Auto Rename Tag 这是一个html标签的插件,可以让你修改一边标签,另外一边自动改变. Beautify 格式化代码插件 Braket Pair Colorizer 给js文件中的每一个 ...
- CentOS下Vim加密解密文本
CentOS用vim/vi给文件加密和解密 一. 利用 vim/vi 加密: 优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了: 缺点:很明显让别人知道加密了,容易让别人把加密的文 ...
- Samba简单应用
一.Samba 简介 1.介绍 Samba(SMB是其缩写) 是一个网络服务器,用于Linux和Windows共享文件之用:Samba 即可以用于Windows和Linux之间的共享文件,也一样用于L ...
- cp 命令
cp - copy files and directories 用法: cp [OPTION] source dest DESCRIPTION Copy SOURCE to DEST, or mult ...
- ufile的硬盘
参考: https://docs.ucloud.cn/compute/uhost/introduction/disk UFS: https://docs.ucloud.cn/storage_cdn/u ...