用递归算法求二叉树的高程序如下:
int treedepth(bitree t)
{
int h,lh,rh;
if(t == NULL)
h = 0;
else{
lh = treedepth(t->lchild);
rh = treedepth(t->rchild);
if(lh>=rh)
h = lh+1;
else
h = rh+1;
}
return h;
}
比如求下面一个二叉树的高:
上面显示的递归算法的过程,主要讲第三部到第四部,当程序执行第三部的时候,这次的运行仍然在第二步调用 treedepth 的过程中,所以程序执行结束后,把 h 返回到第二步递归调用的地方
哎,搞了几天,终于把递归搞熟悉了~