首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >求叶子的数量和树的高度

求叶子的数量和树的高度

作者头像
大忽悠爱学习
发布2021-03-08 11:49:42
发布2021-03-08 11:49:42
7390
举报
文章被收录于专栏:c++与qt学习c++与qt学习

求叶子的数量:递归来求

第一种写法:

代码语言:javascript
复制
//计算叶子的数量
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;
}

第二种写法:

代码语言:javascript
复制
//计算叶子的数量
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;
}

树的高度(深度)

代码语言:javascript
复制
//树的高度
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;
}
代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 求叶子的数量:递归来求
  • 树的高度(深度)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档