二叉树的深度、叶子节点数

二叉树的深度 递归

int DepthOfTree(Node* root)
{
    if(root)
    {
        return DepthOfTree(root->Left)>DepthOfTree(root->Right)?DepthOfTree(root->Left)+1:DepthOfTree(root->Right)+1;
    }
    if( root == NULL )
    {
        return 0;
    }
}

二叉树叶子节点数 递归

int Leafnum(Node* root)
{
    if(root == NULL)
    {
        return 0;
    }
    else if(  (root->Left == NULL) && (root->Right == NULL) )
    {
        return 1;
    }
    else
    {
        return  (Leafnum(root->Left) + Leafnum(root->Right)) ;
    }
}

猜你喜欢

转载自blog.csdn.net/feixi7358/article/details/82898894