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

来源:互联网 发布:大数据4v特征包括 编辑:程序博客网 时间:2024/06/05 04:44



#include<iostream>#include<algorithm>#include<queue>using namespace std;typedef struct node{    char data;    struct node *lchild;    struct node *rchild;} node, *tree;char c[100];int i;void xianxucreat(tree &t){    char a;    a=c[i++];    if(a==',')    {        t=NULL;    }    else    {        t=(tree)malloc(sizeof(node));        t->data=a;        xianxucreat(t->lchild);        xianxucreat(t->rchild);    }}void cengxu(tree &t){    queue<tree>sq;    if(t)        sq.push(t);    while(!sq.empty())    {        t=sq.front();        if(t->lchild==NULL&&t->rchild==NULL)        {            cout<<t->data;        }        sq.pop();        if(t->lchild!=NULL)        {            sq.push(t->lchild);        }        if(t->rchild!=NULL)        {            sq.push(t->rchild);        }    }}int main(){    tree t;    while(cin>>c)    {        i=0;        xianxucreat(t);        cengxu(t);        cout<<endl;    }    return 0;}


0 0
原创粉丝点击