求二叉树中两个节点的最大距离

本文介绍了如何通过递归方式在二叉树中找到两个节点的最大距离。首先定义了二叉树节点的结构体,包含元素、左右孩子以及左右子树的最大距离属性。接着,通过遍历二叉树并更新节点的最大距离属性,最终从叶子节点回溯到根节点,计算出整个二叉树中两个节点的最大距离。文中还给出了具体的测试用例和执行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  二叉树节点的结构体定义如下:

struct BTreeNode
{
 ElemType elem;  // 当前节点元素
 BTreeNode *left; // 左孩子结点
 BTreeNode *right; // 右孩子节点
 int maxLeft;  // 左子树中的最大距离
 int maxRight;  // 右子树中的最大距离
};

使用递归的方式,遍历二叉树,然后从叶子节点开始修改结构体中的 maxLeft 和 maxRight 变量,直至回溯到根节点。

详细的代码如下:

#include <iostream>

using namespace std;

// 将节点保存的数据类型设为整型
typedef int ElemType;

struct BTreeNode
{
	ElemType elem;		// 当前节点元素
	BTreeNode *left;	// 左孩子结点
	BTreeNode *right;	// 右孩子节点
	int maxLeft;		// 左子树中的最大距离
	int maxRight;		// 右子树中的最大距离
};

// 往二叉树中插入节点,该方法实际上最后生成的是二叉查找树
void InsertBTree(BTreeNode **root, ElemType elem)
{
	// 如果树为空,则创建
	if (*root == NULL)
	{
		*root = new BTreeNode;
		(*root)->elem  = elem;
		(*root)->left  = NULL;
		(*root)->right = NULL;
		(*root)->maxLeft  = 0;
		(*root)->maxRight = 0;
		return ;
	}
	
	BTreeNode *node = *root;
	while (node != NULL)
	{
		if (elem < node->elem)
		{
			if (node->left == NULL)
			{
				BTreeNode *temp = new BTreeNode;
				temp->elem  = elem;
				temp->left  = NULL;
				temp->right = NULL;
				temp->maxLeft  = 0;
				temp->maxRight = 0;
				node->left  = temp;
				return ;
			} 
			else
			{
				node = node->left;
			}
		} 
		else
		{
			if (node->right == NULL)
			{
				BTreeNode *temp = new BTreeNode;
				temp->elem  = elem;
				temp->left  = NULL;
				temp->right = NULL;
				temp->maxLeft  = 0;
				temp->maxRight = 0;
				node->right = temp;
				return ;
			} 
			else
			{
				node = node->right;
			}
		}
	}
}

// 声明两个全局变量
BTreeNode *parent = NULL;	// 保存相距最远的两个节点的最近公共父节点
int longestDistance = 0;	// 保存最远的距离值

// 获得树中最长距离的两个节点
void GetLongestDistance(BTreeNode *root)
{
	if (root == NULL)
	{
		return ;
	}

	// 左子树为空的情况,则左子树最大距离为0
	if (root->left == NULL)
	{
		root->maxLeft = 0;
	}
	else
	{
		// 左子树不为空,则递归计算左子树做大距离
		GetLongestDistance(root->left);
	}

	// 右子树为空的情况,则右子树最大距离为0
	if (root->right == NULL)
	{
		root->maxRight = 0;
	}
	else
	{
		// 右子树不为空,则递归计算右子树做大距离
		GetLongestDistance(root->right);
	}

	// 必须递归遍历完左右子树后才能执行下面的判断
	if (root->left != NULL)
	{
		// 取左子树的maxLeft和maxRight中的最大值+1作为该节点的maxLeft
		if (root->left->maxLeft > root->left->maxRight)
		{
			root->maxLeft = root->left->maxLeft + 1;
		}
		else
		{
			root->maxLeft = root->left->maxRight + 1;
		}

	}

	if (root->right != NULL)
	{
		// 取右子树的maxLeft和maxRight中的最大值+1作为该节点的maxRight
		if (root->right->maxLeft > root->right->maxRight)
		{
			root->maxRight = root->right->maxLeft + 1;
		}
		else
		{
			root->maxRight = root->right->maxRight + 1;
		}
	}

	if (longestDistance < root->maxLeft + root->maxRight)
	{
		longestDistance = root->maxLeft + root->maxRight;
		parent = root;
	}

}

void main()
{
	// 定义树的根节点
	BTreeNode *root = NULL;
	
	// 初始化树
	int tree[] = {10, 17, 15, 13, 11, 29, 21, 31, 25};
	for (int i=0; i<sizeof(tree)/sizeof(int); i++)
	{
		InsertBTree(&root, tree[i]);
	}

	// 获得最远距离
	GetLongestDistance(root);

	cout<<"最大距离是:"<<longestDistance<<endl;

	// 找到距离最远的这两个节点
	// 先找左节点
	BTreeNode *temp = parent->left;
	while (temp != NULL)
	{
		if (temp->maxLeft==0 && temp->maxRight==0)	// 叶子节点
		{
			cout<<"左节点是:"<<temp->elem<<endl;
		}

		if (temp->maxLeft > temp->maxRight)
		{
			temp = temp->left;
		} 
		else
		{
			temp = temp->right;
		}
	}
	// 再找右节点
	temp = parent->right;
	while (temp != NULL)
	{
		if (temp->maxLeft==0 && temp->maxRight==0)	// 叶子节点
		{
			cout<<"右节点是:"<<temp->elem<<endl;
		}
		
		if (temp->maxLeft > temp->maxRight)
		{
			temp = temp->left;
		} 
		else
		{
			temp = temp->right;
		}
	}
}



测试使用的二叉树如下:

程序的执行结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值