C语言二叉树的深度指什么?怎么求?

如题所述

第1个回答  2019-04-15
下面是一个用
递归方法
编的二叉树遍历程序,供lz参考。
#include
<stdio.h>//头文件
#include
<stdlib.h>
#include
<malloc.h>
typedef
struct
bitnode
{
char
data;
struct
bitnode
*lchild,*rchild;
}
bitnode,*bitree;//定义结点类型
bitree
createbitree()//创建树
{
char
p;bitree
t;
scanf("%c",&p);
if(p=='
')
t=null;
else
{
t=(bitnode
*)malloc(sizeof(bitnode));//为结点开辟空间
t->data=p;
t->lchild=createbitree();
t->rchild=createbitree();
}
return
(t);
}
void
preorder(bitree
t)//
先序
{
if(t!=null)
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void
inorder(bitree
t)//
中序
{
if(t!=null)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
void
postorder(bitree
t)//
后序
{
if(t!=null)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
void
main()//主函数
{
bitree
ta;
ta=createbitree();
printf("先序遍历:");
printf("\n");
preorder(ta);
printf("\n");
printf("中序遍历:");
printf("\n");
inorder(ta);
printf("\n");
printf("后序遍历:");
printf("\n");
postorder(ta);
}
相似回答
大家正在搜