求叶子的数量和树的高度

求叶子的数量:递归来求

第一种写法:

//计算叶子的数量
int getLeafNum(BinaryNode* root)
{
    
    
	if (root == NULL)
		return 0;
	叶子的数量:这里不能用局部遍量,因为局部遍量生存周期只有在当前函数
     static int num=0;
	//当左子树和右子树都等于NULL时,为叶子
	if (root->lchild == NULL && root->rchild == NULL)
	{
    
    
		num++;
	}
	//通过递归记录有几个叶子
	getLeafNum(root->lchild);
	getLeafNum(root->rchild);
	return num;
}

第二种写法:

//计算叶子的数量
int getLeafNum(BinaryNode* root,int *num)
{
    
    
	if (root == NULL)
		return 0;
	叶子的数量:不能用局部变量,因为局部变量的生命周期之在当前函数
 //   int num=0;
	//当左子树和右子树都等于NULL时,为叶子
	if (root->lchild == NULL && root->rchild == NULL)
	{
    
    
		(*num)++;
	}
	//通过递归记录有几个叶子
	getLeafNum(root->lchild,num);
	getLeafNum(root->rchild,num);
	return *num;
}

在这里插入图片描述

树的高度(深度)

//树的高度
int getTreeHeight(BinaryNode* root)
{
    
    
	//递归到当前函数时,如果结点为空,当前结点一层都不存在
	if (root == NULL)
	{
    
    
		return 0;
	}
	//返回左子树的高度:返回本次递归的当前函数中的左子树高度
	int lheight = getTreeHeight(root->lchild);
	//返回右子树的高度:返回本次递归的当前函数中的右子树高度
	int rheight = getTreeHeight(root->rchild);
	//从左子树到右子树中取最大值加1
	int height = lheight > rheight ? lheight+1 : rheight+1;
	return height;
}

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//二叉树的递归遍历
struct BinaryNode 
{
    
    
	//数据域
	char ch;
	//指针域
	BinaryNode* lchild; //指向左孩子的指针
	BinaryNode* rchild; //指向右孩子的指针
};
//递归遍历:传入根结点指针
void recursion(BinaryNode* root)
{
    
    
	//先序遍历
	if (root == NULL)
		return;
	//先根再左再右
	printf("%c ", root->ch);
	recursion(root->lchild);
	recursion(root->rchild);
}
//计算叶子的数量
int getLeafNum(BinaryNode* root,int *num)
{
    
    
	if (root == NULL)
		return 0;
	叶子的数量:不能用局部变量,因为局部变量的生命周期之在当前函数
 //   int num=0;
	//当左子树和右子树都等于NULL时,为叶子
	if (root->lchild == NULL && root->rchild == NULL)
	{
    
    
		(*num)++;
	}
	//通过递归记录有几个叶子
	getLeafNum(root->lchild,num);
	getLeafNum(root->rchild,num);
	return *num;
}
//树的高度
int getTreeHeight(BinaryNode* root)
{
    
    
	//递归到当前函数时,如果结点为空,当前结点一层都不存在
	if (root == NULL)
	{
    
    
		return 0;
	}
	//返回左子树的高度:返回本次递归的当前函数中的左子树高度
	int lheight = getTreeHeight(root->lchild);
	//返回右子树的高度:返回本次递归的当前函数中的右子树高度
	int rheight = getTreeHeight(root->rchild);
	//从左子树到右子树中取最大值加1
	int height = lheight > rheight ? lheight+1 : rheight+1;
	return height;
}
void output()
{
    
    
	BinaryNode Anode = {
    
     'A',NULL,NULL };
	BinaryNode Bnode = {
    
     'B',NULL,NULL };
	BinaryNode Cnode = {
    
     'C',NULL,NULL };
	BinaryNode Dnode = {
    
     'D',NULL,NULL };
	BinaryNode Enode = {
    
     'E',NULL,NULL };
	BinaryNode Fnode = {
    
     'F',NULL,NULL };
	BinaryNode Hnode = {
    
     'H',NULL,NULL };
	BinaryNode Gnode = {
    
     'G',NULL,NULL };
	//建立关系
	Anode.lchild = &Bnode;
	Anode.rchild = &Fnode;

	Bnode.rchild = &Cnode;
	Cnode.lchild = &Dnode;
	Cnode.rchild = &Enode;

	Fnode.rchild = &Gnode;
	Gnode.lchild =&Hnode;
   //递归遍历算法
	recursion(&Anode);
	printf("\n");
	//叶子数量
	int num = 0;
	printf("叶子的数量:\n");
	printf("%d",getLeafNum(&Anode,&num));
	printf("\n树的高度:\n");
	printf("%d", getTreeHeight(&Anode));
}
int main()
{
    
    
	output();
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_53157173/article/details/114374665