ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

编写递归算法,计算二叉树T中叶子结点的数目。

2026/8/7 23:08:01 拓冰建站 浏览量
编写递归算法,计算二叉树T中叶子结点的数目。

【题目】编写递归算法,计算二叉树T中叶子结点的数目。
二叉链表类型定义∶

typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild,*rchild;

} BiTNode,*BiTree;
要求实现下列函数∶

int Leaves(BiTree T);
/* 计算二叉树T中叶子结点的数目*/

 

#include "allinclude.h"  //DO NOT edit this line
int Leaves(BiTree T) 
{   // Add your code hereint countleaves(BiTree T,int &count);
int count=0;return countleaves( T,count);
}int countleaves(BiTree T,int &count)
{if(T==NULL)return count;if(T->lchild==NULL && T->rchild==NULL)  return ++count;countleaves(T->lchild,count) ;countleaves(T->rchild,count);return count;
}