CompareBinaryTreeDepth

/******************************************************************************************************
  * @file name:		  :CompareBinaryTreeDepth
  * @brief  		  :采用递归的方式来计算二叉树的深度
  * @author 		  :wvjnuhhail@126.com
  * @date 			  :2024/05/03
  * @version 1.0 	  :V1.0
  * @property 		  :暂无
  * @note   		  :None
  * CopyRight (c)  2023-2024   wvjnuhhail@126.com   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
 *  函数作者:    wvjnuhhail@126.com
 *  创建日期:     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;
}

posted @ 2024-05-03 13:22  WJnuHhail  阅读(2)  评论(0编辑  收藏  举报