03-树2 List Leaves

本文介绍了一种树的遍历算法,即广度优先遍历,通过该算法可以按照从上到下、从左到右的顺序输出树的所有叶子节点。文章详细解释了输入格式、输出要求,并给出了具体的样例输入输出以及解题思路。

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.

大意:给你一棵树,你需要按照从上到下,从左到右的顺序输出它的叶子节点(度为0的节点)

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.

大意:第一行是一个正整数N(N<=10)代表树的总的节点个数,树的节点按照输入顺序0到N-1编号,接下来N行代表相对应编号节点的左右孩子节点的编号(‘-’代表空)

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

解释:编号为0的节点,它的左孩子是编号为1的节点,右孩子是空;编号为1的节点,他的左右孩子均为空;编号为2的节点,他的左孩子是编号为0的节点,右孩子是空;

Sample Output:

4 1 5

解题思路:先找到根节点,根节点的特性就是没有双亲,显然,与之对应的就是在输入中没有出现过的编号就是根节点。然后从root结点广度优先搜索,搜索到叶子节点就存入数组v里面,需要注意的是因为要求输出的顺序是从上到下、从左到右,因此如果左右子节点都不为空则应先push左子节点。 

AC代码↓

#include <iostream>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <map>       //STL 映射容器
#include <memory>
#include <cstdio>      //定义输入/输出函数
#include <cstdlib>     //定义杂项函数及内存分配函数
#include <cstring>  
#define maxn 14
#define Null -3
using namespace std;
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;

struct TNode{
    ElementType Data;
    int  Left,Right;
}bt1[maxn],bt2[maxn];

/*用于寻找树的根节点*/
int check[maxn]={0};

int main()
{   //cout << '-'-'0' << endl;
    int n,i,root;
    char left,right;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("\n%c %c",&left,&right);
        bt1[i].Data=i;
        bt1[i].Left=left-'0';
        bt1[i].Right=right-'0';
        if(bt1[i].Left>=0){
            check[bt1[i].Left]=1;
        }
        if(bt1[i].Right>=0){
            check[bt1[i].Right]=1;
        }
    }
    /*找到根节点*/
    for(i=0;i<n;i++){
        if(!check[i]){
            root=i;
            break;
        }
    }
    queue<int> q;
    q.push(root);
    vector<int> v;
    while(!q.empty()){
        int t=q.front();
        q.pop();
        if(bt1[t].Left==Null&&bt1[t].Right==Null){
            v.push_back(t);
        }
        if(bt1[t].Left!=Null){
            q.push(bt1[t].Left);
        }
        if(bt1[t].Right!=Null){
            q.push(bt1[t].Right);
        }
    }
    for(i=0;i<v.size();i++){
        if(i==0){
            printf("%d",v[i]);
        }
        else{
            printf(" %d",v[i]);
        }
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值