03-树1. 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 (<=10) 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
这是一个简单的二叉树,根据题目给出的结点创建一颗树,然后按层次遍历,把叶节点按顺序输出。
1 #include <iostream>
2 #include <cstdlib>
3 #include <set>
4 #include <queue>
5 #include <cstring>
6 #include <string>
7 using namespace std;
8
9 struct node //结点结构体
10 {
11 int leftchild; //左儿子
12 int rightchild; //右儿子
13 node () //初始化
14 {
15 leftchild=-1;
16 rightchild=-1;
17 }
18 };
19 node n[20];
20 bool r[20]; //记录是否是根结点
21 queue<int> q; //有队列遍历树
22 int t=0;
23
24 void traversal(int a) //根据根节点a遍历树并且把也结点输出
25 {
26 q.push(a);
27 while (!q.empty())
28 {
29
30 int j=q.front();
31 //cout <<"q.pop() "<<j<<endl;
32 q.pop();
33 if (n[j].leftchild==-1&&n[j].rightchild==-1)//没有左儿子和有儿子则为叶子
34 {
35 if (t==0)//第一个叶节点直接输出,以后每个结点前有一个空格
36 {
37 cout <<j;
38 t=1;
39 }
40 else
41 {
42 cout <<" "<<j;
43
44 }
45 }
46 if (n[j].leftchild!=-1)
47 {
48 q.push(n[j].leftchild); //左儿子存在,压入队列
49 }
50 if (n[j].rightchild!=-1)
51 {
52 q.push(n[j].rightchild);//左右子存在,压入队列
53 }
54 }
55 }
56 int main()
57 {
58 char a,b;
59 int m;
60 while (cin>>m)
61 {
62 memset(r,false,sizeof(r));
63 for (int i=0;i<m;i++)//根据输入数据构建树
64 {
65 cin>>a>>b;
66 if (a>='0'&&a<='9')
67 {
68 n[i].leftchild=a-48;
69 r[a-48]=true;
70 }
71 if (b>='0'&&b<='9')
72 {
73 n[i].rightchild=b-48;
74 r[b-48]=true;
75 }
76 }
77 for (int i=0;i<m;i++)
78 {
79 if (!r[i])
80 {
81 traversal(i);
82 //cout <<"i"<<i<<endl;
83 cout <<endl;
84 break;
85 }
86 }
87 }
88 return 0;
89 }
03-树1. List Leaves (25)的更多相关文章
- 03-树2. List Leaves (25) 二叉树的层序遍历
03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you a ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- pat03-树2. List Leaves (25)
03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...
- 03-树1 树的同构(25 point(s)) 【Tree】
03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...
- PTA 03-树2 List Leaves (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves (25分) Given a tree, you ...
- 7-4 List Leaves (25分) JAVA
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- L2-006 树的遍历 (25 分)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...
- 03-树2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- 浙大数据结构课后习题 练习三 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. I ...
随机推荐
- Fatal error: Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY' in D:\inetpub\vhosts\zenpty.com\httpdocs\includes\database
打开php.ini配置文件,找到php_pdo_mysql.dll,如果前面有分号";"则表示该行被注释掉了,将分号去掉,保存,然后重启apache服务,重新访问页面,问题解决了.
- [XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...
- 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
python IO操作的时候出现这种错误,检查一些url的目录 这个时候需要注意一下一般的dir举例是:“F:\DOCUMENT\4.7” 需要修改成为:F:/DOCUMENT/4.7
- js 创建对象
1.工厂模式 function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.j ...
- Qt wrappers for OS X Cocoa widgets
Qt wrappers for OS X Cocoa widgetshttps://github.com/MikeMcQuaid/Qocoa
- SQL Server索引的维护 - 索引碎片、填充因子 <第三篇>
实际上,索引的维护主要包括以下两个方面: 页拆分 碎片 这两个问题都和页密度有关,虽然两者的表现形式在本质上有所区别,但是故障排除工具是一样的,因为处理是相同的. 对于非常小的表(比64KB小得多), ...
- codecomb 2085【肥得更高】
题目背景 自2009年以来,A.B站的历史就已经步入了农业变革的黎明期. 在两站的娱乐及音乐区,金坷垃制造业早已得到长足的发展,甚至有些地方还出现了坷垃翻唱的萌芽. 新兴肥料人开始走上历史的舞台. 他 ...
- mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform
问题 解决办法
- canvas arcTo()用法详解 – CodePlayer
canvas arcTo()用法详解 – CodePlayer canvas arcTo()用法详解
- python 性能优化
1.优化循环 循环之外能做的事不要放在循环内,比如下面的优化可以快一倍 2.使用join合并迭代器中的字符串 join对于累加的方式,有大约5倍的提升 3.使用if is 使用if is True比i ...