二叉树节点的结构体定义如下:
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;
}
}
}
测试使用的二叉树如下:
程序的执行结果如下:
