1. 程式人生 > >資料結構實驗之二叉樹七:葉子問題(SDUT 3346)

資料結構實驗之二叉樹七:葉子問題(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;
}