首先要明白,递归的本质其实就是用到栈。
先序,中序,后序三种递归遍历树都可以转为用栈的非递归方式。
下面给出算法,假设栈的功能是已经实现了的。
void Find(Tree *r, int *leaves, int *node)
{
 push(r);
 Tree *t=NULL;
 *leaves = 0;
 *node = 0;
 while ( (t=pop())!= NULL)//假如栈为空,返回NULL
 {
 if (t->left==NULL && t->right==NULL)
 {
 (*leaves)++;
 }
 else
 {
 (*node)++;
 }
 if (t->left != NULL)
 {
 push(t->left);
 }
 if (t->right != NULL)
 {
 push(t->right);
 }
 }
}
                                        
                                      2019-07-17 22:54:05