0
点赞
收藏
分享

微信扫一扫

[杂谈]反义词汇总

鲤鱼打个滚 2023-07-21 阅读 53

⭐️ 前言

✨ 往期文章链接:二叉树的概念性质

上一篇我们讲了二叉树的结构定义,以及前序/中序/后序的递归遍历,还有一些二叉树的接口实现,本篇我们补充一个二叉树的接口 BinaryTreeDepth。✨上一篇文章链接:二叉树详解(1)

前篇:

在这里插入图片描述


在这里插入图片描述


⭐️二叉树的其他接口

// 求二叉树的深度
int BinaryTreeDepth(BinaryTreeNode* root);

BinaryTreeDepth 实现:

int BinaryTreeDepth(BinaryTreeNode* root) {
	// 空树没有高度直接返回0
	if (root == NULL) {
		return 0;
	}
	
	// 递归获取左子树的高度
	int leftTreeDepth = BinaryTreeDepth(root->left);
	// 递归获取右子树的高度
	int rightTreeDepth = BinaryTreeDepth(root->right);

	// 比较左子树和右子树的高度 取较高的同时在加上当前这一层的高度
	return leftTreeDepth > rightTreeDepth ? leftTreeDepth + 1 : rightTreeDepth  + 1;
}

BinaryTreeDepth 递归流程图:

在这里插入图片描述


举报

相关推荐

0 条评论