首页 > 编程语言 >BinaryTree_CountLeafNode

BinaryTree_CountLeafNode

时间:2024-05-04 22:45:21浏览次数:14  
标签:BinaryTree CountLeafNode n1 n2 root 节点

/******************************************************************************************************
  * @file name:		  :StacksSimulateQueue
  * @brief  		  :两个栈实现队列的功能
  * @author 		  :[email protected]
  * @date 			  :2024/05/04
  * @version          :V1.0
  * @property 		  :暂无
  * @note   		  :None
  * CopyRight (c)  2023-2024   [email protected]   All Right Reseverd
******************************************************************************************************/
#include <stdio.h>
#include <stdbool.h>


typedef struct insertSort
{
    struct insertSort* lchild;
    struct insertSort* rchild;
}Tnode_t;

/********************************
 *  funName:     BinaryTree_CountLeafNode
 *  funtion:     计算一颗二叉树的所有叶子节点的数量        
 *  Argument:
 *               @n1  :  n1记录左子树叶子节点
 *               @n2  :  n2记录右子树叶子节点
 * 
 *  返回结果:     二叉树的深度,类型为整型
 *  注意事项:     None
 *  函数作者:    [email protected]
 *  创建日期:     2024/05/04
 *  修改历史:    None
 *  函数版本:    V1.0
 * ********************************/

int BinaryTree_CountLeafNode(Tnode_t *root)
{
    //n1记录左子树叶子节点
    int n1;

    //n2记录右子树叶子节点
    int n2;

    //递归函数提前编写终止条件
    if(NULL == root)
    {
        //空树的情况,返回0
        return 0;
    }
    else if(root -> lchild == NULL && root -> rchild == NULL)
    {
        //说明只有一个根节点,那么根节点就是叶子节点
        return 1;
    }
    else{

        //二叉树有子树,需要计算左子树的叶子节点和右子树的叶子节点
        n1 = BinaryTree_CountLeafNode(root -> lchild);
        n2 = BinaryTree_CountLeafNode(root -> rchild);
    }
}





标签:BinaryTree,CountLeafNode,n1,n2,root,节点
From: https://www.cnblogs.com/hhail08/p/18172870

相关文章

  • CompareBinaryTreeDepth
    /*******************************************************************************************************@filename: :CompareBinaryTreeDepth*@brief :采用递归的方式来计算二叉树的深度*@author :[email protected]*@date :2024/05/03*......
  • 【11月LeetCode组队打卡】Task3--BinaryTree
    树基本术语:节点的度:叶子节点=0分支节点:含有的子树个数节点关系:父,子,兄节点层次:根节点:1floor路径:两节点间经过的节点序列路径长度:路径上的边数树的分类:节点子树是否可以互换位置:有序树:从左到右各子树依次有序(不能互换无序树二叉......