【データ構造】二分木の詳細説明(2)

⭐️序文

✨ 過去の記事へのリンク:二分木の概念的な性質

前の記事では、バイナリ ツリーの構造定義、 preorder/inorder/postorderの再帰的走査、およびバイナリ ツリーのいくつかのインターフェイス実装について説明しましたが、この記事では、バイナリ ツリー インターフェイスを追加します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再帰的なフローチャート:

ここに画像の説明を挿入


おすすめ

転載: blog.csdn.net/cccyi7/article/details/131814961