这是我的BinaryTree<T>类代码里的一个递归成员函数,
求指定结点在指定子树中的深度,你在调用时把subTree
改成root,就是求你所说的第一个问题了.
//////////////////////////////////////////////////
//Depth()私有成员
//计算指定结点p在子树subTree中的深度
//////////////////////////////////////////////////
template<class T>
int BinaryTree<T>::Depth(BinTreeNode<T>* p
            ,BinTreeNode<T>* subTree)
{
    //如果子树不空
    if(subTree!=NULL)
    {
        //如果就是根结点则深度为1
        if(p==subTree)
            return 1;
        else
        {
            //递归求p在左子树里的深度
            int ld=Depth(p,subTree->leftChild);
            //递归求p在右子树里的深度
            int rd=Depth(p,subTree->rightChild);
            if(ld!=0)
                //左子树深度加1
                return 1+ld;
            else if(rd!=0)
                //右子树深度加1
                return 1+rd;
            else
                //没有找到则返回0
                return 0;
        }
    }
    else
        //空则深度为0
        return 0;
};
///////////////////////////////////Depth()函数结束
[[it] 本帖最后由 geninsf009 于 2008-11-16 20:56 编辑 [/it]]