03-树2 List Leaves (25 分)(树的层序遍历queue及bfs)

03-树2 List Leaves (25 分)
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 (≤10) 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:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5

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

输入规格:
每个输入文件包含一个测试用例。 对于每种情况,第一行给出正整数N(≤10),它是树中节点的总数 - 因此节点从0到N-1编号。 然后是N行,每行对应一个节点,并给出节点左右子节点的索引。 如果孩子不存在,将在该位置放置“ - ”。 任何一对孩子都被一个空间隔开。

输出规格:
对于每个测试用例,按照自上而下和从左到右的顺序在一行中打印所有叶子的索引。 任何相邻数字之间必须只有一个空格,并且该行末尾没有额外的空格。

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5

题解:本题就是树的层序遍历,就像bfs一样,运用结构队列就可以。每次存储队首元素的左右儿子节点,再取出队首元素进行判断是不是叶子结点,是就输出,然后再删掉队首元素,继续取出下一个队首元素将其左右儿子节点压入队列,直到队列为。遍历完毕。

判断叶子节点的方法:在输入时判断一下他有么有左右,都没有说明他就是叶子结点。

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 1000005
#define mod 7654321

struct Tree
{
    int Left;
    int Right;
    int isLeaf=0;//判断是不是叶子节点
}T[20];

int n;

int BuildTree(Tree *T)
{
    int Root=-1;
    int check[1000];
    char l,r;
    cin>>n;
    if(n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>l>>r;
            if(l!='-')
                T[i].Left=l-'0',check[T[i].Left]=1;
            else
                T[i].Left=-1;
            if(r!='-')
                T[i].Right=r-'0',check[T[i].Right]=1;
            else
                T[i].Right=-1;

            if(l=='-'&&r=='-')//若左右都无则为叶子节点
                T[i].isLeaf=1;
        }
        int i;
        for(i=0;i<n;i++)//若没有指向当前元素的节点,则为根节点
            if(!check[i])
                break;
        Root=i;
    }
    return Root;
}

queue<int> q;

int inQueue(Tree *T,int R)
{
    q.push(R);
    int first=1;
    while(!q.empty())
    {
        int temp=q.front();//保存队首元素的值
        Tree F=T[temp];//保存队首的节点

        if(F.isLeaf&&first==0)//是叶节点就输出,严格控制空格位置
            cout<<" "<<temp;
        if(F.isLeaf&&first)
            cout<<temp,first=0;

        q.pop();//删除队首元素
        if(F.Left!=-1)//将当前队首元素的左右节点压入队列中
            q.push(F.Left);
        if(F.Right!=-1)
            q.push(F.Right);
    }
}

int main()
{
    int R;
    R=BuildTree(T);
    inQueue(T,R);
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值