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. SpringBoot四种读取properties文件的方式

    环境:IDEA jdk1.8 SpringBoot2.1.4 例,如下默认application.properties文件   一.使用`@ConfigurationProperties`注解将配置文 ...

  2. java.lang.NullPointerException错误的解决方案

    java.lang.NullPointerException空指针异常是像我一样新手很容易出现的问题,这个问题一般情况都是不细心的时候出现的,开始正文如下: 1.业务层面的错误: a.没有写非空验证: ...

  3. 在LINUX上查询哪个用户从哪个IP登录,登录时间,执行了什么命令?

    在/etc/profile里面加入以下代码 PS1="`whoami`@`hostname`:"'[$PWD]' history USER_IP=`>/dev/null| a ...

  4. Python学习之路基础篇--07Python基础+编码、集合 和 深浅Copy

    1 小数据池(节省内存) 只有数字和字符串中内存,储存在同一个内存地址中 数字的范围在-5~256中,字符串有以下两个特点:不能含有特殊字符,还有就s*20 不是同一个地址,而要*21就不是了 2 编 ...

  5. cf549B Looksery Party 贪心

    题目大意:有n个员工,每个员工通讯录里有自己的号码和其他一些员工的号码.现在有若干员工参加一个聚会,他们会给自己通讯录里所有的人发一条短信,包括自己.现在有个人预测了每个员工会收到多少条短信,而你要寻 ...

  6. 9. Web browser-related (网页浏览器相关 4个)

    Firefox NoScript是Firefox的插件,可以阻止JavaScript,Java,Flash和其他插件内容(允许您选择性地重新启用某些站点). 它还提供跨站点脚本保护. 这主要是为了保持 ...

  7. TCP 三次握爪 四次挥手

    TCP三次握手和四次挥手过程 1.三次握手 (1)三次握手的详述 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源.Client端接收到ACK报文后也向 ...

  8. CodeForce 517 Div 2. C Cram Time

    http://codeforces.com/contest/1072/problem/C C. Cram Time time limit per test 1 second memory limit ...

  9. re正则匹配城市名

    匹配城市名称,只要第一次出现之后的数据 import re a='巴州区白云县台公交乡公司对面区海蜃楼6楼' b=re.search(r'(^\w+?区)|(^\w+?县)|(^\w+?镇)',a). ...

  10. TensorFlow机器学习实战指南之第二章

    一.计算图中的操作 在这个例子中,我们将结合前面所学的知识,传入一个列表到计算图中的操作,并打印返回值: 声明张量和占位符.这里,创建一个numpy数组,传入计算图操作: import tensorf ...