试分别采用递归和非递归方式编写两个函数,求一棵给定二叉树中叶子节点的个数

2025-06-28 21:36:14
推荐回答(2个)
回答1:

//递归算法
int LeafCount(Tree *tree)
{ if(tree->lchild!=null)
return LeafCount(tree->lchild) + LeafCount(tree->rchild);
else
return 1;
}
非递归算法可以用一个中间栈实现

回答2:

不会