Road_7 发表于 2014-9-5 17:18:19

递归二叉树深度不知哪里错了

#include <stdio.h>
#include <stdlib.h>

int m , n;
typedef struct tree                 //define
{
        char zhi ;
        struct tree *Lchild;
        struct tree *Rchild;
}ErTree;


void CreateTree(ErTree *Tree)                        //build a Tree
{
        char a ;
        scanf("%c",&a);
        if( a == '#')
        Tree = NULL;
        else
        {
                Tree = ( ErTree *)malloc(sizeof (ErTree));
                Tree->zhi= a;
                CreateTree(Tree->Lchild);
                CreateTree(Tree->Rchild);
               
        }

}


int Deep( ErTree*Tree)         //求深度
{               
       

        if (NULL == Tree)
                return 0 ;
        else
        {
                m = Deep(Tree->Lchild);
                n = Deep(Tree->Rchild);
               
                if( m >= n)
                       return        (m+1);
               else
                       return(n+1) ;        
        }
}



int main()                       
{
        int c ;
       ErTreeTree , *pTree;
        CreateTree(&Tree);
        c =Deep(&Tree);
        printf("%d",c);
       
        return 0 ;
}

流行语 发表于 2014-9-6 10:56:27

#include <stdio.h>
#include <stdlib.h>

int m, n;
typedef struct tree               //define
{
      char zhi ;
      struct tree *Lchild;
      struct tree *Rchild;
}ErTree,*PErTree;
/************************************************************************/
/*
初始化树根节点
*/
/************************************************************************/
void InitTree(PErTree* root)
{
        *root = (PErTree)malloc(sizeof(struct tree));
        (*root)->zhi = 0;
        (*root)->Lchild = NULL;
        (*root)->Rchild = NULL;
}

/************************************************************************/
/*
build a Tree
*/
/************************************************************************/
void CreateTree(ErTree** Tree)
{
       char a ;
       fflush(stdin);
       scanf("%c",&a);
       if(a == '#')
       {
               return ;
       }
       else
       {
               (*Tree)->Lchild = (ErTree *)malloc(sizeof (struct tree));
               (*Tree)->Lchild->zhi = a;
               (*Tree)->Lchild->Lchild = NULL;
               (*Tree)->Lchild->Rchild = NULL;
               CreateTree(&((*Tree)->Lchild));
               
               fflush(stdin);
               scanf("%c",&a);
               if( a == '#')
               {
                       return ;
               }
               (*Tree)->Rchild = (ErTree *)malloc(sizeof (struct tree));
               (*Tree)->Rchild->zhi = a;
               (*Tree)->Rchild->Lchild = NULL;
               (*Tree)->Rchild->Rchild = NULL;
               CreateTree(&((*Tree)->Rchild));
       }
}


int Deep( ErTree*Tree)         //求深度
{   
       int lChildDeep,rChildDeep;
       if (NULL == Tree)
               return 0 ;
        else
        {
                lChildDeep = Deep(Tree->Lchild);
                rChildDeep = Deep(Tree->Rchild);
                return (lChildDeep > rChildDeep)?(lChildDeep+1):(rChildDeep+1);
        }
}



int main()                        
{
       int c ;
       ErTree* Tree;
       InitTree(&Tree);
       CreateTree(&Tree);
       c =Deep(Tree);
       printf("%d",c);
       
       return 0 ;
}
你看一下。

阔怀 发表于 2015-8-16 16:05:59

厉害
页: [1]
查看完整版本: 递归二叉树深度不知哪里错了