二叉树以二叉链表存储,统计其中叶子结点值为x的数目
int count=0;
int calleaf(BiTree root,int x){
if(root==NULL) return 0;
if((root.data==x)&&(root.lchild==NULL)&&(root.rchild==NULL))
count++;
calleaf(root.lchild,x);
calleaf(root.rchild,x);
return count;
}
使用了全局变量计数,递归先序遍历二叉树。
通过本题掌握二叉树的递归遍历方法,本类题均可用类似方法处理。