数据结构实验之二叉树七:叶子问题(SDUT 3346)

本文介绍了一种使用递归方法构建二叉树的算法,并实现了一个用于打印完全二叉树叶子节点的功能。通过输入字符串形式的二叉树结构,程序能够创建相应的二叉树并输出其叶子节点的值。
#include <bits/stdc++.h>
using namespace std;
struct node
{
    char data;
    struct node *lc, *rc;
};
char a[100];
int num = 0;
struct node *creat()
{
    struct node *root;
    if(a[num++] == ',')
    {
        root = NULL;
    }
    else {
        root = new node;
        root -> data = a[num - 1];
        root -> lc = creat();
        root -> rc = creat();
    }
    return root;
};
void fin(struct node *root)
{
    struct node *q[100];
    int in = 0, out = 0;
    q[in ++] = root;
    while(out < in){
    if(q[out])
    {
       if(q[out] -> lc == NULL && q[out] -> rc == NULL) printf("%c",q[out]->data);
        q[in++] = q[out]->lc;
        q[in++] = q[out] -> rc;
    }
    out ++;
    }
}
int main()
{
    while(~scanf("%s", a))
    {
        struct node *root;
        num =0;
        root = creat();
        fin(root);
        printf("\n");
    }
    return 0;
}


 

转载于:https://www.cnblogs.com/lcchy/p/10139449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值