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:

 -
- -
- - -
- -
-

Sample Output:

  

给定一棵树,您应该按照自上而下和从左到右的顺序列出所有叶子。

 #include <stdio.h>
#include <queue>
#include <algorithm>
#define MaxTree 11
#define Null -1
using namespace std; int check[MaxTree];
int P[MaxTree];
int num=;
queue<int> qu; struct TreeNode
{
int Left;
int Right;
}T[MaxTree]; int BuildTree(struct TreeNode T[])
{
int N,i;
char le,ri;
int Root;
scanf("%d\n",&N);
if(N)
{
for(i=;i<N;i++)
{
check[i]=;
}
for(i=;i<N;i++)
{
scanf("%c %c\n",&le,&ri);
if(le!='-')
{
T[i].Left=le-'';
check[T[i].Left]=;
}
else T[i].Left=Null;
if(ri!='-')
{
T[i].Right=ri-'';
check[T[i].Right]=;
}
else T[i].Right=Null;
}
for(i=;i<N;i++)
{
if(!check[i]) break;
}
Root = i;
}
else
{
Root=Null;
}
return Root;
} void search(int Tree)
{
if(Tree!=Null)
{
qu.push(Tree);
while(!qu.empty())
{
int k;
k=qu.front();
if(T[k].Left==Null&&T[k].Right==Null)
P[num++]=k;
qu.pop();;
if(T[k].Left!=Null)
qu.push(T[k].Left);
if(T[k].Right!=Null)
qu.push(T[k].Right);
}
}
else return;
} int main()
{
int Tree;
Tree=BuildTree(T);
search(Tree);
int i;
for(i=;i<num;i++)
{
if(i==)
printf("%d",P[i]);
else printf(" %d",P[i]);
}
return ;
}

List Leaves的更多相关文章

  1. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  2. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  3. LeetCode - 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  4. LeetCode Sum of Left Leaves

    原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...

  5. Leetcode: Find Leaves of Binary Tree

    Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...

  6. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. 366. Find Leaves of Binary Tree

    Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...

  8. 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...

  9. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. Find Leaves of Binary Tree

    Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...

随机推荐

  1. spring cloud_1_mm_eureka2 eureka集群

    一个eureka会出现单点故障 这里整两个 eureka--1 application.yml: #注册中心端口 server: port: 8888 #唯一标示eureka注册中心 方便互相识别 e ...

  2. python 异步发送邮件 aiosmtplib

    aiosmtplib is an asynchronous SMTP client for use with asyncio.文档地址 与 smtplib的用法大体相同 有几个地方需要注意下: 加密S ...

  3. shell脚本登录远程服务器并下载至本地

    通常有这样备份的需求,将远程服务器的代码或者数据打包压缩然后下载到本地路径 实现方式 需要对远程服务器实现无密码访问,通过配置公钥实现: 使用ssh执行命令然后转向到本地的方法一步完成打包和下载,可参 ...

  4. android 事件绑定

    layout布局设计了页面,如何绑定事件,与用户进行交互需要在Activity中进行处理. 下面的layout,有两个按钮. <LinearLayout android:layout_width ...

  5. SVM问题汇总

    1.为什么要选择最大间隔分类器,请从数学角度上说明? 答:几何间隔与样本的误分次数间存在关系: 其中的分母就是样本到分类间隔距离,分子中的R是所有样本中的最长向量值 2.样本失衡会对SVM的结果产生影 ...

  6. 使用hibernate原生sql查询,结果集全为1的问题解决

    问题如下: String sqlTest ="select summary,summaryno from F_Summary"; List<Map<Object, Ob ...

  7. Oracle数据库 基础SQL语句练习

    一.说明 第一次使用Oracle,想做一些练习,熟悉一些oracle. 表:使用的是scott用户,默认的表 具体表讲解,可以参考该文档:https://www.cnblogs.com/xjcheng ...

  8. Python 特殊方法

    1 _int_ 定义在类中,创建类的实例的时候回先调用此方法,用于对该类的一些初始化(例如变量初始化) 2 _str_ def _str_(self): return "this is a ...

  9. VritualBox安装CentOS及安装增强工具图文教程

    一  VM VirtualBox VirtualBox可在官网https://www.virtualbox.org/下载,完全免费. 二  安装CentOS系统(以CentOS6.9为例) 1.Cen ...

  10. vue cli3.0配制axios代理

    今天学习时,想访问网易新闻接口,结果显而易见,因为跨域被浏览器拒绝了. 去网上找一下结果一开始找到的是2.x版本的配置,生硬的放进去,给我各种报错.编译阶段就炸了.浪费好多时间 再按3.0版本去搜索才 ...