ch5_60.c

来自「数据结构( C语言版) 讲义daima数据结构( C语言版) 讲义daima」· C语言 代码 · 共 44 行

C
44
字号
#include <alloc.h>
#include <stdio.h>

typedef struct node
{   char data;
    struct node *lchild,*rchild;
}JD;

JD *crt_bt_pre(JD *bt)
{  char ch;
   printf("ch=");
   scanf("%c",&ch);
   getchar();
   if(ch==' ')  bt=NULL;
   else
   {   bt=(JD *)malloc(sizeof(JD));
       bt->data=ch;
       bt->lchild=crt_bt_pre(bt->lchild);
       bt->rchild=crt_bt_pre(bt->rchild);
   }
   return(bt);
}
void treedepth(JD *bt,int *l,int *h)
{
    int l1=0,l2=0,h1=0,h2=0;
    if(bt!=NULL)
    {  (*l)++;
       if (*l>*h) *h=*l;
       treedepth(bt->lchild,&l1,&h1);
       treedepth(bt->rchild,&l2,&h2);
       if (h1>h2)  *h=*h+h1;
       else	*h=*h+h2;

    }
}
void main()
{  /* ABC00DE0G00F000  */
    JD *head=NULL;
    int level=0,high=0;
   head=crt_bt_pre(head);
   treedepth(head,&level,&high);
   printf("depth of tree is %d\n",high);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?