最近在学二叉树,二叉树就是一个究极递归套娃过程,在字符串章节是学过、C语言也是学过,我是同时学小甲鱼和王道的课程。
创建二叉树:采用先序遍历的建立方式,就是先找跟节点,然后非空就建立左节点,然后建立右节点。
计算二叉树深度:这个递归算法想了好久不理解,大概意思是,首先先序遍历,一直找到最后一个叶子节点,此时l=0.r=0.于是max=0+1;之后返回上一层,此时l=1,然后继续+1,这样遍历之后一层一层返回,一层一层+1,就计算出了左子树的深度,右子树也是如此 。
#include <stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
char data; /*结点的数据域*/
struct BiTNode* lchild, * rchild; /*指向左孩子和右孩子*/
} BiTNode, * BiTree;
/*创建一棵二叉树*/
void CreatBiTree(BiTree* T)
{
char c;
scanf_s("%c", &c);
if (c == '#')
*T = NULL;
else
{
*T = (BiTNode*)malloc(sizeof(BiTNode)); /*创建根结点*/
(*T)->data = c; /*向根结点中输入数据*/
CreatBiTree(&((*T)->lchild)); /*递归地创建左子树*/
CreatBiTree(&((*T)->rchild)); /*递归地创建右子树*/
}
}
//计算二叉树的深度
int getBitreeDepth(BiTree T)
{
int leftHeight, rightHeight, maxHeight;//左子树,右子树,最大深度
if (T != NULL) //如果为空树
{
leftHeight = getBitreeDepth(T->lchild);//左子树深度
rightHeight = getBitreeDepth(T->rchild);//右子树深度
maxHeight = leftHeight > rightHeight ? leftHeight : rightHeight;//最大深度
return maxHeight + 1;//二叉树深度=最大深度+1
}
else
{
return 0;
}
}
void main()
{
BiTree T = NULL; /*最开始T指向空*/
printf("请您输入一个二叉树(以#为空子树):\n");
CreatBiTree(&T); /*创建二叉树*/
printf("\n二叉树的深度为%d\n", getBitreeDepth(T));
getchar();
}