在这里插入代码片
#include "stdio.h"
#include "malloc.h"
typedef struct node{
char data;
struct node *lchild,*rchild;
}Tree ,*btree;
void creat(btree *t){
char a;
scanf("%c",&a);
if(a==' '){
*t=NULL;
}else{
*t=(btree)malloc(sizeof(btree));
(*t)->data=a;
creat(&((*t)->lchild));
creat(&((*t)->rchild));
}
}
//先序遍历:根节点,左子树,右子树
void display(btree t,int len){
if(t){
printf("%c位于第%d层\n",t->data,len);
qian(t->lchild,len+1);
qian(t->rchild,len+1);
}
}
//二叉树深度
int m,n;//全局变量
int depth(btree t){
if(t==NULL){
return 0;
}else{
m=depth(t->lchild);
n=depth(t->rchild);
if(m>n){
return(m+1);
}else{
return(n+1);
}
}
}
int main(){
btree t=NULL,t1;
int len=1,l;
creat(&t);
printf("二叉树:\n");
printf("输出:\n");
display(t,len);
printf("\n");
l=depth(t);
printf("二叉树深度:%d\n",l);
printf("\n");
return 0;
}
03-04