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. ADO.Net中DataSet的应用

    一.知识点描述 1.DataSet是ADO.NET的中心概念.可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合.也就是说,即使断开数据链路,或者关闭数据库,Data ...

  2. 随手小代码——Python 从集合中随机抽取元素

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  3. eclipse使用技巧心得分享

    eclipse使用技巧心得分享   习惯了eclipse开发java程序,公司最近的项目都是idea开发的,同时android studio也是idea原型开发的,在学android开发,所以脱离ec ...

  4. mysql 数据库

    show databases 显示库use databse 进入库(databse为库名)show tables 显示表create table a select * from b where 0=1 ...

  5. EEG 睡眠 节律 代码

    a1=load('EEG01.txt');[c,r]=size(a1);z=10;%等于几,绘图起点从几开始s=256*z;%绘图起点;还有,这里的256是采样率d=floor(c/256);cn=d ...

  6. xcode打包真机测试

    背景:xocode版本低于真机版本 解决方法:

  7. Day1-浅谈接口测试

    1.接口测试的定义:从数据库里面取数据,插入数据,把前段和后端联系起来: 2.接口测试的必要性: 1)前端未开发好,但是接口已开发 2)页面上测不了的,可以通过接口来测试 3.接口测试需要注意: 1) ...

  8. ssh 端口更改或ssh 远程接不上的问题(尤其是国外服务器)

    问题: Connecting to 149.*.*.*:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]' ...

  9. 关于jstl中碰到的Property 'username' not found on type java.lang.String异常

    在jstl的forEach循环的时候总是有异常,刚开始以为是把类的属性名打错了,因为显示的是Property not found,但就算从类文件里面复制属性名过来依然显示的是Property not ...

  10. Python 斗地主发牌

    #coding = utf-8 import random def Creat_Card(): card_type = ['♥','♠','♦','♣'] card_values= ['A', '2' ...