判断是否为满树

来源:互联网 发布:mac看不了acfun 编辑:程序博客网 时间:2024/05/10 16:20

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct node

{

         char data;

         struct node *lchild,*rchild;

};

typedef struct node NODE;

int n=0,len;       

char a[100];

int main()

{

         NODE * creat();

         int judge(NODE* root);

         NODE *root;

         scanf("%s",a);

         len=strlen(a);

         root=creat();

         if(judge(root)==0)

         {

                   printf("yes\n");

         }

         else

         {

                   printf("no\n");

         }

         return 0;

 

}

 

int judge(NODE* root)           // 递归判断是否为满树

{

         int a,b;

         if(root==NULL)

         {

                   return 0;

         }

         else

         {

                   if(root->lchild!=NULL&&root->rchild==NULL)

                   {

                            return 1;

                   }

                   else if(root->lchild==NULL&&root->rchild!=NULL)

                   {

                            return 1;

                   }

                   else

                   {

                            if((a=judge(root->lchild))==0&&(b=judge(root->rchild)==0))

                            {

                                     return 0;

                            }

                            else

                            {

                                     return 1;

                            }

                   }

         }

}

 

NODE * creat()                    //类似前序遍历创建二叉树

{

         NODE *t;

         t=(NODE *)malloc(sizeof(NODE));

         if(n>=len)

         {

                   t=NULL;

         }

         if(a[n]!='#')

         {

            t->data=a[n];

            n++;

            t->lchild=creat();

            t->rchild=creat();

         }

         else

         {

                   n++;

                   t=NULL;

         }

         return t;

}