编写递归算法,计算二叉树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 here
 
int 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;
}

最近更新

  1. TCP协议是安全的吗?

    2024-01-19 15:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-19 15:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-19 15:38:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-19 15:38:01       20 阅读

热门阅读

  1. 学习 SSR(Server-Side Rendering)的心得和体会

    2024-01-19 15:38:01       28 阅读
  2. 【算法详解】力扣179.最大数

    2024-01-19 15:38:01       34 阅读
  3. 力扣(leetcode)第824题山羊拉丁文(Python)

    2024-01-19 15:38:01       34 阅读
  4. LeetCode 15. 三数之和

    2024-01-19 15:38:01       31 阅读
  5. Springcloud:HV000183

    2024-01-19 15:38:01       27 阅读
  6. Ubuntu/linux c开发(8)linux ping 命令解析

    2024-01-19 15:38:01       32 阅读