浙大数据结构课后习题 练习三 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 ...
随机推荐
- 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_3 响应之返回值是void类型
定义先的标签 返回void 测试默认的跳转 虽然是404但是方法执行了. 默认请求了 testVoid.jsp这个页面.请求路径叫什么 就访问哪个jsp页面. 使用request请求转发 抛出的异常 ...
- SQL Server 2005 和自增长主键identity说再见——NEWSEQUENTIALID()(转载)
在SQL Server 2005环境下,表的主键应该怎样设计.目前主要用到的主键方案共三种: 自动增长主键 手动增长主键 UNIQUEIDENTIFIER主键 1.先说自动增长主键,它的优点是简单,类 ...
- C#编程 Excel操作
使用OLEDB操作Excel,关于OLEDB介绍参考http://www.cnblogs.com/moss_tan_jun/archive/2012/07/28/2612889.html 连接字符串: ...
- nginx +域名
文件nginx.config: server { listen 80 ; server_name dns19.hichina.com;location / { proxy_pass http://12 ...
- PJzhang:我发现一个有两个答案的数独题
猫宁!!! 最近做数独题,发现了一个答案不唯一的数独,之前对此类数独有所耳闻,但是没有亲手发现,碰巧发现一个,很是欣喜. 下面展示了两个答案 第一个 第二个 绿色标签是答案 ...
- 自然语言处理工具HanLP-N最短路径分词
本篇给大家分享baiziyu 写的HanLP 中的N-最短路径分词.以为下分享的原文,部分地方有稍作修改,内容仅供大家学习交流! 首先说明在HanLP对外提供的接口中没有使用N-最短路径分词器的,作者 ...
- JavaScript中好用的对象数组去重
对象数组去重 Demo数据如下: var items= [{ "specItems": [{ "id": "966480614728069122&qu ...
- ASP.NET Core中使用EF Core(MySql)Code First
⒈添加依赖 MySql.Data.EntityFrameworkCore ⒉在appsettings.json配置文件中配置数据库连接字符串 { "Logging": { &quo ...
- 虚拟机上安装Linux系统之ubuntu
以前自己在虚拟机上安装过几回Linux系统,有centos.ubuntu,不过都没来得及写一个安装教程,今天正好需要重新安装一下,就分享一个安装ubuntu的详细教程 安装前准备: VMWare虚拟机 ...
- JAVA二维码编码&解码
QRCodeUtil.java package web; import java.awt.AlphaComposite; import java.awt.Color; import java.awt. ...