浙大数据结构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表示有N个结点且结点的编号为0-N-1,下面N行表示第N-1个结点的左孩子和右孩子的索引(即编号),不难发现没有出现的编号就是根节点的编号因为根节点既不是别人的左孩子也不是别人的右孩子,这道题就是让你找出既没有左孩子又没有右孩子的结点编号,并且是按照树上从上到下从左到右的顺序

思路:首先按照从上到下从左到右的顺序输出就需要使用层序遍历整棵树即使用队列来实现,那么就需要先找出根节点的编号,再使用结构数组将每个结点的左孩子和右孩子存入数组,最后再使用队列先push根节点再判断此结点有无左右孩子,如果都没有就按顺序存入leave数组,如果有左孩子就push左孩子,如果有右孩子就push右孩子,最后输出leave数组即可,详见代码

#include <iostream>
#include <queue>
#define Null -1
using namespace std;
const int N = 100;
int check[N];
int leave[N];
struct TreeNode
{
    int l/*左孩子*/,r/*右孩子*/;
}a[N];
int FindRoot()
{
    int root = Null;/*初始化*/
    int n;
    cin >> n;/*节点个数*/
    if(n)
    {
        for(int i = 0 ; i < n ; i++)
        {
            check[i] = 0;
        }
        for(int i = 0; i < n ; i++)
        {
            char cl,cr;
            cin >> cl >> cr;
            if(cl != '-')/*如果左孩子不为空*/
            {
                a[i].l = cl - '0';
                check[a[i].l] = 1;/*标记左孩子*/
            }
            else/*如果左孩子为空*/
            {
                a[i].l = Null;
            }
            if(cr != '-')/*如果右孩子不为空*/
            {
                a[i].r = cr - '0';
                check[a[i].r] = 1;/*标记右孩子*/
            }
            else/*如果右孩子为空*/
            {
                a[i].r = Null;
            }
        }
        int i;
        for(i = 0 ; i < n ; i++)
        {
            if(!check[i])/*找到一个既不是左孩子又不是右孩子的结点即为根结点*/
                break;
        }
        root = i;
    }
    return root;
}
void PrintLeava(int root)
{
    if(root == Null)/*如果根结点为空证明是空树*/
    {
        cout << "-1" << endl;
        return;
    }
    queue<int>q;
    q.push(root);
    int i = 0;
    while(!q.empty())
    {
        int s = q.front();
        q.pop();
        if(a[s].l == Null && a[s].r == Null)/*如果该结点既没有左孩子又没有右孩子就是叶子*/
        {
            leave[i++] = s;
        }
        else
        {
            if(a[s].l != Null)/*如果左孩子不为空就push左孩子*/
            {
                q.push(a[s].l);
            }
            if(a[s].r != Null)/*如果右孩子不为空就push右孩子*/
            {
                q.push(a[s].r);
            }
        }
    }
    for(int j = 0; j < i ; j++)/*按顺序输出叶子*/
    {
        if(j < i - 1)
        {
            cout << leave[j] << " ";
        }
        else
        {
            cout << leave[j];
        }
    }
}
int main()
{
    int root = FindRoot();/*找到根结点*/
    PrintLeava(root);
    return 0;
}

发布了18 篇原创文章 · 获赞 4 · 访问量 316

猜你喜欢

转载自blog.csdn.net/leslie___/article/details/105654160
今日推荐