浙大数据结构课后习题 练习三 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 ...
随机推荐
- IOS input框轻点无效修复方法
FastClick.prototype.focus = function(targetElement) { targetElement.focus();//加入这一句话就OK了 };
- 内网gitlab访问外网
外网要访问内网,内网出口必须有公网ip,且外网访问内部网络可以用花生壳,可以用主机端口映射 写ansible调api,打通内网到阿里云的master节点 如何将公司内网与云服务器内网打通 参照 ...
- 有关face的好文MARK集
Single Stage Headless Face Detector 据说已经超过了tiny face DDFD <Multi-view Face Detection Using Deep C ...
- EncryptionAndDecryptionC# 加密 解密
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MSSQL数据库日志满的快速解决办法
先提供一种复杂的方法压缩日志及数据库文件如下: 1.清空日志 DUMP TRANSACTION 库名 WITH NO_LOG 2.截断事务日志: BACKUP LOG 数据库名 WITH NO_ ...
- sshpass密码
使用sshpass sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path ...
- Nginx动态添加模块 平滑升级
已经安装好的Nginx动态添加模块 说明: 已经安装好的Nginx,需要添加一个未被编译安装的模块,需要怎么弄呢? 这里已安装第三方nginx-rtmp-module模块为例 nginx的模块是需要重 ...
- VS显示代码行号
工具>文本编辑器>常规 如图勾选行号选项,点击确定即可显示行号. 如果没有行号的选项,则需先勾选下图中左下角的‘’显示所有设置选‘’‘项.
- Kick Start 2019 Round F Teach Me
题目链接 题目大意 有 $N$ 个人,$S$ 项技能,这些技能用 $1, 2, 3, \dots, S$ 表示 .第 $i$ 个人会 $c_i$ 项技能($ 1 \le c_i \le 5 $).对于 ...
- ASP.NET Core中使用Autofac进行属性注入
一些无关紧要的废话: 作为一名双修程序员(自封的),喜欢那种使用Spring的注解形式进行依赖注入或者Unity的特性形式进行依赖注入,当然,形式大同小异,但结果都是一样的,通过属性进行依赖注入. A ...