03-树1. List Leaves (25)

时间:2022-04-07 07:51:32

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)的更多相关文章

  1. 03-树2&period; List Leaves &lpar;25&rpar; 二叉树的层序遍历

    03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you a ...

  2. L2-006 树的遍历 (25 分&rpar; (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  3. pat03-树2&period; List Leaves &lpar;25&rpar;

    03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...

  4. 03-树1 树的同构(25 point&lpar;s&rpar;) 【Tree】

    03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...

  5. PTA 03-树2 List Leaves &lpar;25分&rpar;

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...

  6. 7-4 List Leaves &lpar;25分&rpar; JAVA

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  7. L2-006 树的遍历 (25 分&rpar;

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  8. 03-树2 List Leaves &lpar;25 分&rpar;

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  9. 浙大数据结构课后习题 练习三 7-4 List Leaves &lpar;25 分&rpar;

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

随机推荐

  1. 自定义Sublime Text的图标

    sublime text很赞,windows上最接近mac逼格的轻量编辑器,对于我这样比较喜欢格调的人来说,简直不二之选啊. 美中不足的是,看久了觉得它的图标似乎不是很上心.现在都流行扁平化了而它还停 ...

  2. css初始化代码方案

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-24) 为了消除各浏览器对css默认的设置,保持网页在各浏览器中的外观保持一致,初始化css就显得非常必要了!很多时候 ...

  3. 用vlc SDK创建一个播放视频文件和RTSP流视频的Demo

    #include <stdio.h> #include <tchar.h> #include <time.h> #include <windows.h> ...

  4. CodeForces 510E Fox And Dinner

    网络流. 原点到偶数连边,容量为2, 奇数到汇点连边,容量为2, 偶数到与之能凑成素数的奇数连边,容量为1 如果奇数个数不等于偶数个数,输出不可能 如果原点到偶数的边不满流,输出不可能 剩下的情况有解 ...

  5. jQuery Datetable 渲染

    渲染器 有些情况下,使用表时,表中的行的数据源不包含您希望在表中直接显示的值.您可能希望将其转换为不同的表示形式(时间戳为人类可读的格式),合并数据点(名字和姓氏)或对该值执行一些计算(计算营业额和费 ...

  6. Kubernetes Ingress Controller的使用及高可用落地

    Kubernetes Ingress Controller的使用及高可用落地 看懂本文要具备一下知识点: Service实现原理和会应用 知道反向代理原理,了解nginx和apache的vhost概念 ...

  7. JD-GUI反编译出现ERROR

    反编译Jar包一般使用JD-GUI工具,很方便.但有时会出现异常,如下: 此时,可以辅助使用另一个工具来反编译代码:Luyten(https://github.com/deathmarine/Luyt ...

  8. Session变量在PHP中的使用

    PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...

  9. CAN总线要点

    前言 CAN总线的应用在现在看来越来越广泛,我厂设备从最初的ARM9与ARM7平台.期间升级过度到CortexA8与Cortex M3平台,再到现在的Cortex M4平台,围绕CAN进行了一系列产品 ...

  10. Elasticsearch跨集群搜索&lpar;Cross Cluster Search&rpar;

    1.简介 Elasticsearch在5.3版本中引入了Cross Cluster Search(CCS 跨集群搜索)功能,用来替换掉要被废弃的Tribe Node.类似Tribe Node,Cros ...