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. epoll+socket实现 socket并发 linux服务器

    /* 实现功能:通过epoll, 处理多个socket * 监听一个端口,监听到有链接时,添加到epoll_event * xs */ #include <stdio.h> #includ ...

  2. swiper遇到的问题

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  3. C# 树状图

    效果图: 结构: frmMain层 using hierarchy.BLL; using hierarchy.Model; using System; using System.Collections ...

  4. Java中对List集合的常用操作

    目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...

  5. python基础(五)

    一.os模块 import os# os.rename(old,new)#重命名# os.remove(f)#删除文件 # os.mkdir('china/beijing') #创建文件夹# os.m ...

  6. PA教材提纲 TAW12-2

    Unit1 Adjustment of SAP Standard Software(SAP标准软件修改) 1.1 Adjusting SAP Standard Software(如何修改SAP标准软件 ...

  7. Jump跳板机的搭建和部署

    Jump跳板机简绍   概括 Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. Jumpserver ...

  8. web渗透学习方向

    本章写给新加入我们破晓工作室的学弟学妹. 我现在写的是渗透方向的学习方向.因为我参加了线上培训班,听了专门培训渗透的课程后.所以感觉我们工作室自学太累了.如果没有一个“正确”的学习方向都不知道该学些什 ...

  9. 浅谈兔兔对Object.defineProperty的理解

    给一个对象定义一个新的属性或者在修改一个对象现有的属性,并返回这个对象 语法: Object.defineProperty(参数1,参数2,参数3) 参数1:目标对象 参数2:要修改或者添加的属性名称 ...

  10. PTA1

    1-1 数组定义中,数组名后是用方括号括起来的常量表达式,不能用圆括号. (1分) [T ] F 1-2 在C语言中能逐个地使用下标变量,也能一次引用整个数组. (1分) T [F]因为它有首地址 1 ...