03-树2 List Leaves (25 分)

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

本体需要使用队列来进行层次遍历,叶节点的左右为Null。

//
// Created by wangzhiyong on 18-9-20.
//
#include <iostream>
#include <vector>
#include <queue> //本体需要使用队列进行层次遍历
#define MaxTree 10
#define ElementType char
#define Tree int
#define Null -1
using namespace std;
queue<int> q;

struct TreeNode //树结构体
{
    ElementType Element;
    Tree Left;
    Tree Right;
}T1[MaxTree]; //结构体数组

Tree BuildTree(struct TreeNode T[]);
void Leaves(Tree R1,vector<int> &Result1);//将得到的节点依次存到Result1中
int main() {
    Tree R1,R2;
    vector<int> Result;
    R1 = BuildTree(T1);
    Leaves(R1,Result);
    for (int i = 0; i <Result.size() ; ++i) {

        if(i == Result.size()-1)
        {cout<<Result[i];

        }else{
            cout<<Result[i]<<" ";
        }
    }


}

Tree BuildTree(struct TreeNode T[])
{
    int N,Root = -1;
    cin>>N;//输入总的节点数
    int check[N]; //N的数组用来辅助判断节点有没有父节点,有父节点则为1,为0的为根节点
    if(N) //判断输入的是不是真的
    {
        for (int k = 0; k < N; ++k) {
            check[k] = 0; //循环赋值为0
        }
        for (int i = 0; i < N; ++i)
        {
            char cl,cr;
            cin>>cl>>cr;
            if(cl != '-')
            {
                T[i].Left = cl -'0';//转换时需要注意,如果直接转换结果为asc码里数字的值。
                check[T[i].Left] = 1; //这个节点的做节点有父节点
            }else{
                T[i].Left = Null;
            }

            if(cr != '-')
            {
                T[i].Right = cr -'0';
                check[T[i].Right] = 1; //这个节点的有右节点有父节点
            }else{
                T[i].Right = Null;
            }
        }

        for (int j = 0; j < N; ++j) {
            if(!check[j]){ //非 0为真,1为假
                Root = j;
            }
        }

    }
    return Root;
}
void Leaves(Tree R1,vector<int> &Result1) {
    Tree temp;//临时的节点
    if (R1 == Null) {//如果输入不是一个树则退出
        return;
    }
    q.push(R1);//把头节点压入队列
    while (!q.empty()) { //循环直到q为空
        temp = q.front(); //在MOOC中的DeleteQ等于这两行
        q.pop();//把旧节点取出
        if ((T1[temp].Left == Null) && (T1[temp].Right == Null)) {
            Result1.push_back(temp);//如果为叶节点
        }
        if (T1[temp].Left != Null) {
            q.push(T1[temp].Left);
        }
        if (T1[temp].Right != Null) {
            q.push(T1[temp].Right);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值