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

来源:互联网 发布:好易网络电视下載 编辑:程序博客网 时间:2024/05/22 04:25

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

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>using namespace std;char a[1006];int k;struct node{    int data;    struct node *lchild,*rchild;};struct node *creat(){    struct node*root;    char c;    c=a[k++];    if(c==',')        return NULL;    else    {        root = (struct node*)malloc(sizeof(struct node));    root->data=c;        root->lchild=creat();        root->rchild=creat();    }       return root;};void CountTree(struct node *root){    if(!root)        return;///注意判断,否则会越界,RE    int pu = 0,po = 0;    struct node *p[111];    p[pu++] = root;    while(pu > po)    {        if((!p[po]->lchild)&&(!p[po]->rchild))            printf("%c",p[po]->data);            //cout<<p[po]->data;        if(p[po]->lchild)            p[pu++] = p[po]->lchild;        if(p[po]->rchild)           p[pu++] =p[po]->rchild;        po++;    }}int main(){    while(cin>>a)    {        struct node *root;        k = 0;///注意归零       root= creat();        CountTree(root);        cout<<endl;    }    return 0;}
0 0
原创粉丝点击