鱼C论坛

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

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

[复制链接]
发表于 2014-9-5 17:18:19 | 显示全部楼层 |阅读模式
1鱼币
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int m , n;
  4. typedef struct tree                 //define
  5. {
  6.         char zhi ;
  7.         struct tree *Lchild;
  8.         struct tree *Rchild;
  9. }ErTree;


  10. void CreateTree(ErTree *Tree)                        //build a Tree
  11. {
  12.         char a ;
  13.         scanf("%c",&a);
  14.         if( a == '#')
  15.         Tree = NULL;
  16.         else
  17.         {
  18.                 Tree = ( ErTree *)malloc(sizeof (ErTree));
  19.                 Tree->zhi= a;
  20.                 CreateTree(Tree->Lchild);
  21.                 CreateTree(Tree->Rchild);
  22.                
  23.         }

  24. }


  25. int Deep( ErTree  *Tree)         //求深度
  26. {               
  27.        

  28.         if (NULL == Tree)
  29.                 return 0 ;
  30.         else
  31.         {
  32.                 m = Deep(Tree->Lchild);
  33.                 n = Deep(Tree->Rchild);
  34.                
  35.                 if( m >= n)
  36.                          return        (m+1);
  37.                  else  
  38.                          return  (n+1) ;        
  39.         }
  40. }



  41. int main()                       
  42. {
  43.         int c ;
  44.          ErTree  Tree , *pTree;
  45.         CreateTree(&Tree);
  46.         c =Deep(&Tree);
  47.         printf("%d",c);
  48.        
  49.         return 0 ;
  50. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-6 10:56:27 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int m, n;
  4. typedef struct tree                 //define
  5. {
  6.         char zhi ;
  7.         struct tree *Lchild;
  8.         struct tree *Rchild;
  9. }ErTree,*PErTree;
  10. /************************************************************************/
  11. /*
  12. 初始化树根节点
  13. */
  14. /************************************************************************/
  15. void InitTree(PErTree* root)
  16. {
  17.         *root = (PErTree)malloc(sizeof(struct tree));
  18.         (*root)->zhi = 0;
  19.         (*root)->Lchild = NULL;
  20.         (*root)->Rchild = NULL;
  21. }

  22. /************************************************************************/
  23. /*
  24. build a Tree
  25. */
  26. /************************************************************************/
  27. void CreateTree(ErTree** Tree)
  28. {
  29.          char a ;
  30.          fflush(stdin);
  31.          scanf("%c",&a);
  32.          if(a == '#')
  33.          {
  34.                  return ;
  35.          }
  36.          else
  37.          {
  38.                  (*Tree)->Lchild = (ErTree *)malloc(sizeof (struct tree));
  39.                  (*Tree)->Lchild->zhi = a;
  40.                  (*Tree)->Lchild->Lchild = NULL;
  41.                  (*Tree)->Lchild->Rchild = NULL;
  42.                  CreateTree(&((*Tree)->Lchild));
  43.                  
  44.                  fflush(stdin);
  45.                  scanf("%c",&a);
  46.                  if( a == '#')
  47.                  {
  48.                          return ;
  49.                  }
  50.                  (*Tree)->Rchild = (ErTree *)malloc(sizeof (struct tree));
  51.                  (*Tree)->Rchild->zhi = a;
  52.                  (*Tree)->Rchild->Lchild = NULL;
  53.                  (*Tree)->Rchild->Rchild = NULL;
  54.                  CreateTree(&((*Tree)->Rchild));  
  55.          }
  56. }


  57. int Deep( ErTree*Tree)         //求深度
  58. {   
  59.          int lChildDeep,rChildDeep;
  60.          if (NULL == Tree)
  61.                  return 0 ;
  62.         else
  63.         {
  64.                 lChildDeep = Deep(Tree->Lchild);
  65.                 rChildDeep = Deep(Tree->Rchild);
  66.                 return (lChildDeep > rChildDeep)?(lChildDeep+1):(rChildDeep+1);
  67.         }
  68. }



  69. int main()                        
  70. {
  71.          int c ;
  72.          ErTree* Tree;
  73.          InitTree(&Tree);
  74.          CreateTree(&Tree);
  75.          c =Deep(Tree);
  76.          printf("%d",c);
  77.          
  78.          return 0 ;
  79. }
复制代码

你看一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-8-16 16:05:59 | 显示全部楼层
厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-18 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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