计算二叉树的深度

 

 

先遍历二叉树的左子树的深度,然后再遍历二叉树右子树的深度。最后判断左子树和右子树的深度,如果左子树比右子树深则返回左子树深度+1,否则返回右子树深度+1。

 

 

/* 初始条件: 二叉树T存在。操作结果: 返回T的深度 */

int BiTreeDepth(BiTree T)
{
 int i,j;
    if(!T)
  return 0;
 if(T->lchild)
  i=BiTreeDepth(T->lchild); // 左子树深度
 else
  i=0;
 if(T->rchild)
  j=BiTreeDepth(T->rchild);  // 右子树深度
 else
  j=0;
 return i>j?i+1:j+1;
}
posted @ 2015-06-07 23:35  stone531  阅读(572)  评论(0编辑  收藏  举报