7-4 List Leaves

本文介绍了一种通过广度优先搜索遍历树结构的方法,并实现按从上到下、从左到右的顺序打印所有叶子节点的功能。输入包括树的节点数及各节点的左右子节点信息,输出为按照指定顺序排列的所有叶子节点的索引。

7-4 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

AC代码如下:

#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Node
{
    int data;
    char left;
    char right;
}T[20];
int n = 0;
int root = -1;
void build()
{
    char cl, ch;
    cin >> n;
    int i, j, k;
    int book [n];

    for (i = 0; i < n; i++)
    {
        book[i] = 0;
    }

    for (j = 0; j < n; j++)
    {
        cin >> cl >> ch;
        T[j].data = j;
        if (cl == '-')
        {
            T[j].left = -1;
        }
        else
        {
            T[j].left = cl - '0';
            book[cl-'0'] = 1;
        }
        if (ch == '-')
        {
            T[j].right = -1;
        }
        else
        {
            T[j].right = ch - '0';
            book[ch-'0'] = 1;
        }
    }
    for (k = 0; k < n; k++)
    {
        if (book[k] != 1)
        {
            root = k;
            break;
        }
    }
}

void traversal()
{
    queue <int> q;
    q.push(T[root].data);
    int arr[20];
    int i = 0;
    while (!q.empty())
    {
        int item = q.front();
        arr[i] = item;
        q.pop();
        if (T[item].left != -1)
        {
            q.push(T[item].left);
        }
        if (T[item].right != -1)
        {
            q.push(T[item].right);
        }
        i++;
    }
    bool flag = false;
    for (int j = 0; j < i; j++)
    {
        if (T[arr[j]].left == -1 && T[arr[j]].right == -1)
        {
            if(flag)
                cout << " ";
            cout << T[arr[j]].data;
            flag = true;
        }
    }
}
int main()
{
    build();
    traversal();
    return 0;
}

 

在PTA平台上,关于List的使用有多个相关题目涉及到不同的操作。 在单链表操作方面,有从单链表 `list` 中删除第 `i` 个元素的题目。当需要对链表进行全面遍历,且要处理头节点以及后续所有节点时,会使用 `p = L` 这种方式进行遍历。例如打印链表所有节点的信息,包括头节点中可能存储的一些链表相关的元数据(如链表长度等),就需要从 `L` 开始遍历 [^1]。 还有在单链表 `list` 的第 `i` 个位置上插入元素 `x` 的题目。需要编写程序将 `n` 个整数插入初始为空的单链表,第 `i` 个整数插入在第 `i` 个位置上,这里的 `i` 代表位序,从 1 开始。插入结束后,要输出链表长度,并顺序输出链表中的每个结点的数值。并且还需尝试将最后一个整数插入到链表的第 0 个、第 `n + 2` 个位置上,以测试错误信息的输出 [^4]。 另外,有关于树的 `List` 相关题目,如 “PTA 03 - 树2 List Leaves”,要求给定一棵树,按从上到下、从左到右的顺序列出所有叶子节点 [^2]。 在链表反转方面,“PTA Reversing Linked List” 题目要求对于每个测试用例,输出反转后的有序链表,每个节点占一行,且输出格式与输入相同 [^3]。 ### 示例代码(单链表插入) ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def insert_at_position(head, i, x): new_node = ListNode(x) if i == 1: new_node.next = head return new_node current = head count = 1 while current and count < i - 1: current = current.next count += 1 if current: new_node.next = current.next current.next = new_node return head # 示例用法 head = None n = 3 values = [1, 2, 3] for i in range(1, n + 1): head = insert_at_position(head, i, values[i - 1]) current = head while current: print(current.val) current = current.next ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值