问题遇到的现象和发生背景
为什么这个深度求不出来呀?
问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *lchild,*rchild;
}binnode;
int creatbin(binnode *t)//初始化
{
char ch;
scanf("%c",&ch);
if(ch=='#')//遇到#置空
{
t=NULL;
}
else
{
t=(binnode *)malloc(sizeof(binnode));//建立空间;
t->data=ch;
creatbin(t->lchild);//创建左
creatbin(t->rchild);//创建右
}
return 1;
}
int depth(binnode *t)//
{
int m=0,n=0;
if(t==NULL)
return 0;
else
{
printf("abcdefg ");
m=depth(t->lchild);
n=depth(t->rchild);
if(m>n)
return (m+1);//m>n时,才m+1
else
return (n+1);//两个都为空,也返回n+1;
}
}
void main()
{
binnode t;
printf("创建是否成功:%d\n",creatbin(&t));
printf("节点深度为:%d",depth(&t));
}