PTA 03-树2 List Leaves

题目描述如下:

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

大致意思就是:输入节点个数,然后按顺序(0到N-1)输入每个节点的左右子树的索引,要求输出叶子节点,顺序为从上到下,从左到右。

这便是案例造出的树。

解决方案

        1、创建树:由于是二叉树,我们可以选择用结构体数组去存树的节点以及左右孩子的索引。

struct treenode{
    int data;
    int left;
    int right;
}tree[100];
int buidtree()//造树函数,并返回树根节点
{
    bool st[100];//判断节点是否被访问,一次都没被访问的就是树根节点。
    memset(st,0,sizeof(st));
    int n;
    cin >> n;
    for(int i = 0;i < n;i++)
    {
        char l,r;
        cin >> l >> r;
        tree[i].data = i;
        if(l == '-')
        {
            tree[i].left = -1;
        }
        else
        {
            tree[i].left = l-'0';
            st[l-'0'] = 1;
        }
        if(r == '-')
        {
            tree[i].right = -1;
        }
        else
        {
            tree[i].right = r-'0';
            st[r-'0'] = 1;
        }
    }
    for(int i = 0;i < n;i++)
    {
        if(!st[i])
        {
            return i;//返回树根
        }
    }
}

        2、如何遍历树,并按要求输出叶子节点:由于题目要求是从上到下,从左到右,这意味着在遍历当前层的节点之前,需要先遍历完上一层的节点,这显然是层序遍历。层序遍历基于队列的数据结构实现,这里我使用的是数组模拟队列。

vector<int> arr;


void printl(int h)//h便是根节点
{
    int a[100];
    int tt = -1,ll = 0;//模拟队列的头和尾
    a[++tt] = h;//入队
    while(ll < tt+1)//判断非空
    {
    	int ans = a[ll++];
    	if(tree[ans].left == -1 && tree[ans].right == -1)
	    {
	        arr.push_back(tree[ans].data);//将符合要求的节点放入vector中
	    }
	    if(tree[ans].left != -1) a[++tt] = tree[ans].left;
	    if(tree[ans].right != -1) a[++tt] = tree[ans].right;
	}
    return ;
}

        注意:这里对输出有要求,所以先将符合要求的节点放入vector中,最后一起输出。

全部代码展示

#include<bits/stdc++.h>
using namespace std;
struct treenode{
    int data;
    int left;
    int right;
}tree[100];
vector<int> arr;
int buidtree()
{
    bool st[100];
    memset(st,0,sizeof(st));
    int n;
    cin >> n;
    for(int i = 0;i < n;i++)
    {
        char l,r;
        cin >> l >> r;
        tree[i].data = i;
        if(l == '-')
        {
            tree[i].left = -1;
        }
        else
        {
            tree[i].left = l-'0';
            st[l-'0'] = 1;
        }
        if(r == '-')
        {
            tree[i].right = -1;
        }
        else
        {
            tree[i].right = r-'0';
            st[r-'0'] = 1;
        }
    }
    for(int i = 0;i < n;i++)
    {
        if(!st[i])
        {
            return i;
        }
    }
}
void printl(int h)
{
    int a[100];
    int tt = -1,ll = 0;
    a[++tt] = h;
    while(ll < tt+1)
    {
    	int ans = a[ll++];
    	if(tree[ans].left == -1 && tree[ans].right == -1)
	    {
	        arr.push_back(tree[ans].data);
	    }
	    if(tree[ans].left != -1) a[++tt] = tree[ans].left;
	    if(tree[ans].right != -1) a[++tt] = tree[ans].right;
	}
    return ;
}
int main()
{
    int head = buidtree();
    printl(head);
    for(int i = 0;i < arr.size();i++)
    {
        if(i > 0)
        {
            cout << ' ' << arr[i] ;//格式要求处理。
        }
        else
        {
            cout << arr[i];
        }
    }
    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值