寻找二叉树 以某个值为根的点的 深度,先序创树
//先序创建树,初始化树
typedef struct Btree{
char data;
struct Btree *lc;
struct Btree *rc;
}*Btree,node;//树结点 结构体
Btree CT()
{
char c;
Btree t;
scanf("%c",&c);
if(c == '#')
{
//如果# 说明该结点 不存在
t = NULL;
}
else
{
t = (Btree)malloc(sizeof(node));
t->data = c;//根
t->lc = CT();//左
t->rc = CT();//右
}
return t;
}
//寻找二叉树 以某个值为根的点的 深度
int dep(Btree t)//获取 以t为根结点树的 深度
{
if(t == NULL)
{
return 0;
}
m = dep(t->lc);//左子树深度
n = dep(t->rc);//右子树深度
return m > n ? m:n + 1;//返回最深的子树深度
}
void getdep(Btree t,char c)//寻找值为c的结点,并获取以其为根的子树 深度
{
if(t == NULL)
{
return;
}
if(t->data == c)
{
printf("第一个值为c的结点 深度为%d",dep(t));
exit(1);
}
else
{
getdep(t->lc);
getdep(t->rc);
}
return;
}