03-树2 List Leaves(25 point(s)) 【Tree】

该篇博客介绍了如何按层次顺序打印二叉树的叶子节点。给定一棵树的节点数,通过输入每个节点的左右子节点信息,利用广度优先搜索(BFS)策略,遇到没有子节点的节点时将其加入结果列表。示例输入和输出展示了具体操作过程。
摘要由CSDN通过智能技术生成

03-树2 List Leaves(25 point(s))

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

思路

广度优先搜索 往下搜 遇到 没有儿子的 压入 一个 vector 就可以

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

struct Node
{
    int l, r;
}q[10];

queue <int> Q;

vector <int> ans;

void bfs()
{
    int len = Q.size();
    for (int i = 0; i < len; i++)
    {
        int num = Q.front();
        Q.pop();
        if (q[num].l != -1 && q[num].r != -1)
        {
            Q.push(q[num].l);
            Q.push(q[num].r);
        }
        else if (q[num].l != -1)
            Q.push(q[num].l);
        else if (q[num].r != -1)
            Q.push(q[num].r);
        else
            ans.pb(num);
    }
    if (Q.size())
        bfs();
}

int main()
{
    int n;
    scanf("%d", &n);
    char a, b;
    map <int, int> m;
    for (int i = 0; i < n; i++)
    {
        scanf(" %c %c", &a, &b);
        if (a != '-')
        {
            q[i].l = a - '0';
            m[a - '0'] = 1;
        }
        else
            q[i].l = -1;
        if (b != '-')
        {
            q[i].r = b - '0';
            m[b - '0'] = 1;
        }
        else
            q[i].r = -1;
    }
    int root;
    for (int i = 0; i < n; i++)
    {
        if (m[i] == 0)
        {
            root = i;
            break;
        }
    }
    Q.push(root);
    bfs();
    vector <int>::iterator it;
    for (it = ans.begin(); it != ans.end(); it++)
    {
        if (it != ans.begin())
            printf(" ");
        printf("%d", (*it));
    }
    printf("\n");
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值