浙大数据结构课后习题 练习三 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 ...
随机推荐
- Linux scp 免密码 传输文件
Linux scp 免密码 传输文件 背景介绍 最近项目是集群化部署(由 node1,node2,node3 三台 CentOS 7.4 的虚拟机构成). 但是,涉及到跨机器同步文件的问题,想通过写s ...
- 《Javascript 语言精粹》 中 用到的一些代码 (1)
var isNumber = function isNumber(value){ return typeof value === 'number' && isFinite(value) ...
- 【转】HBASE Region in Transition issue on Master UI
[From]https://community.hortonworks.com/content/supportkb/244808/hbase-region-in-transition-issue-on ...
- 网络实验 04-利用三层交换机实现VLAN间路由
利用三层交换机实现VLAN间路由 一.实验目标 掌握交换机Tag VLAN 的配置 掌握三层交换机基本配置方法 掌握三层交换机VLAN路由的配置方法 通过三层交换机实现VLAN间相互通信 二.实验背景 ...
- Linux文件权限码
权限 二进制值 八进制值 描述 --- 000 0 没有任何权限 --x 001 1 只有执行权限 -w- 010 2 只有写入权限 -wx 011 3 有写入和执行权限 r-- 100 4 只有读取 ...
- Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)
Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- 【Qt开发】QT对话框去掉帮助和关闭按钮 拦截QT关闭窗口的CloseEvent
建了一个对话框,我不想把边框去掉,只想去掉关闭按钮, setWindowFlags(windowFlags()&~Qt::WindowCloseButtonHint&~Qt::Wind ...
- win10安装tensorflow (cpu版)
前提: 下载anaconda,然后创建一个python虚拟环境: 命令: conda create -n tf_cpu python=3.6 # (tf_cpu 是这个虚拟环境的名字) ...
- SpringBoot 使用Mybatis操作mysql示例
1.准备数据库 创建数据库 create databases baodanjia; 创建帐号 create user 'baodanjia'@'%' identified by '123456' gr ...
- PTA(Basic Level)1023.组个最小数
给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就 ...