浙大数据结构课后习题 练习三 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 ...
随机推荐
- c#/netcore/mvc视图中调用控制器方法
1: public class HomeController : Controller { public ActionResult Index() { ...
- PHP+实现文件的上传和下载
工程截图 配置路径 修改系统配置文件路径 填写正确的项目路径 将loclahost:811/up6/改为实际项目路径. 文件和文件夹批量上传 当网络问题导致传输错误时,只需要重传出错分片,而不是整个文 ...
- 参考:菜菜的sklearn教学之降维算法.pdf!!
PCA(主成分分析法) 1. PCA(最大化方差定义或者最小化投影误差定义)是一种无监督算法,也就是我们不需要标签也能对数据做降维,这就使得其应用范围更加广泛了.那么PCA的核心思想是什么呢? 例如D ...
- 微信小程序遍历wx:for,wx:for-item,wx:key
微信小程序中wx:for遍历默认元素为item,但是如果我们设计多层遍历的时候我们就需要自定义item的字段名以及key的键名 wx:for="{{item.goodsList}}" ...
- linux上wps2019不能启动解决方法
本人linux上的wps2016升级成wps2019后启动不了,双击图标没反应,在命令行输入wps,提示"/lib64/libc.so.6: version `GLIBC_2.18' not ...
- 【学习笔记】adb命令
1.adb connect 连接设备 如:adb connect 127.0.0.1:62001 连接夜神模拟器\adb connect 127.0.0.1:21503 链接逍遥模拟器 adb con ...
- Git命令与介绍
一. Git[命令与介绍] 1. 作用 可以用于个的项目版本控制和管理 目前多用于团队间的多人协作项目开发 2. 安装 l 安装包路径 l 安装 后的路径: 3. 工作流程 (1) 创建项目目录 在路 ...
- react中路由跳转push与replace的区别
路由跳转,replace / push 区别 push: a-b-c 可以回到上一级 例: this.props.history.push('路由地址') replace: a-b-c 回不到上一级 ...
- ASP.NET Core中使用EF Core(MySql)Code First
⒈添加依赖 MySql.Data.EntityFrameworkCore ⒉在appsettings.json配置文件中配置数据库连接字符串 { "Logging": { &quo ...
- PageObject 页面对象模式
一.PageObject 页面对象设计模式 (一个页面建一个类,即对象,页面对象) 每个页面都建对应的class,类中包含了页面的输入框.标题.元素等元素,测试代码中测试这个页面时,只需要调用这个页 ...