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. Delphi7第三方控件

    控件安装(安装时建议先关闭Delphi) 1.只有一个DCU文件的组件. DCU文件是编译好的单元文件,这样的组件是作者不想把源码公布.一般来说,作者必须说明此组件适合Delphi的哪种版本,如果版本 ...

  2. C# Ini、Json、Xml 封装类

    1.Ini是什么?(我对它的理解,用于存储用户配置信息的文件,该文件放在用户电脑...) INI文件是一个无固定标准格式的配置文件.它以简单的文字与简单的结构组成,常常使用在Windows操作系统,或 ...

  3. 浅析 java ArrayList

    浅析 java ArrayList 简介 容器是java提供的一些列的数据结构,也可以叫语法糖.容器就是用来装在其他类型数据的数据结构. ArrayList是数组列表所以他继承了数组的优缺点.同时他也 ...

  4. Selenium之table操作

    操作内容: 获取table总行数.总列数.获取某单元格的text值,删除一行[如果每行后边提供删除的按钮] HTML代码: <html><head><meta http- ...

  5. c++ complie link error 2019/2001

    1:没有将需要的文件添加到项目中,只是考到项目所在的目录了 2:没有添加 类导出 宏

  6. 【linux基础】ubuntu系统NVIDIA驱动安装

    在安装GPU环境下的软件工具,特别是CUDA/CUDNN等,一定要先把GPU环境搭建好. NVIDIA驱动安装会遇到各种问题,真希望黄教主可以将各个工具如何安装使用讲解的更加细致.清楚一些,有时候按照 ...

  7. linux 修改配色

    PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ " ORvim ~/ ...

  8. Introduction tp Operating System

    一.虚拟化 为了让用户告诉操作系统如何利用虚拟机功能,OS提供给应用程序一些接口——系统调用,也会说提供了一个标准库. CPU通过分时达到虚拟化. 内存物理模型只是一个字节数组,读写修改需要制定地址. ...

  9. JS如何充分“压榨”浏览器

    不同浏览器厂商实现的 JS 标准有所不同,这意味着 window 对象和可用的 api 也有所不同,希望不久的将来,所有浏览器都能实现统一的 JS 标准. 自己写 api 是很耗费时间跟精力的,而且变 ...

  10. Ubuntu使用总结一

    一.安装 Ubuntu桌面版与服务器版的不同之处桌面版面向个人电脑使用者,可以进行文字处理.网页浏览.多媒体播放和玩游戏.本质上说,这是一 个为普通用户所定制的多用途操作系统.另一方面,服务器版旨在充 ...