首页 > 其他分享 >CompareBinaryTreeDepth

CompareBinaryTreeDepth

时间:2024-05-03 13:33:59浏览次数:11  
标签:GetDepth CompareBinaryTreeDepth 二叉树 深度 n1 n2 root

/******************************************************************************************************
  * @file name:		  :CompareBinaryTreeDepth
  * @brief  		  :采用递归的方式来计算二叉树的深度
  * @author 		  :[email protected]
  * @date 			  :2024/05/03
  * @version 1.0 	  :V1.0
  * @property 		  :暂无
  * @note   		  :None
  * CopyRight (c)  2023-2024   [email protected]   All Right Reseverd
******************************************************************************************************/
#include<stdio.h>
#include <stdbool.h>

//定义结构体
typedef struct CompareBinaryTreeDeep
{
    //左孩子
    struct CompareBinaryTreeDeep* lchild;

    //右孩子
    struct CompareBinaryTreeDeep* rchild;
}Tnode_t;


/********************************
 *  funName:     BinaryTree_GetDepth
 *  funtion:     计算二叉树的深度(递归)           
 *  Argument:
 *               @n1  :  记录左子树的深度
 *               @n2  :  记录右子树的深度
 *  返回结果:     二叉树的深度,类型为整型
 *  注意事项:     None
 *  函数作者:    [email protected]
 *  创建日期:     2024/05/03
 *  修改历史:    None
 *  函数版本:    V1.0
 * ********************************/

int BinaryTree_GetDepth(Tnode_t *root)
{
    
    int n1;         //记录左子树的深度

    int n2;         //记录右子树的深度


    //1.由于采用递归函数,首先写好终止的条件
    if(NULL == root)
    {
        return 0;       //若为空树,返回0
    }
    else{
    //说明是子树的二叉树,分别计算左子树与右子树,取两者的最大值
        n1 = BinaryTree_GetDepth(root->lchild);

        n2 = BinaryTree_GetDepth(root->rchild);
    }

    //返回值+1:加上根节点所在的层数
    return ((n1 > n2)?n1:n2) + 1;
}

标签:GetDepth,CompareBinaryTreeDepth,二叉树,深度,n1,n2,root
From: https://www.cnblogs.com/hhail08/p/18171143

相关文章