鱼C论坛

 找回密码
 立即注册
查看: 4394|回复: 2

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

[复制链接]
发表于 2014-9-5 17:18:19 | 显示全部楼层 |阅读模式
1鱼币
#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 ;
         ErTree  Tree , *pTree;
        CreateTree(&Tree);
        c =Deep(&Tree);
        printf("%d",c);
        
        return 0 ;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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 ;
}
你看一下。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-16 16:05:59 | 显示全部楼层
厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-7 20:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表