转载并参考July的博客http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html,万分感谢!
题目:
写一个程序,求一棵二叉树中相距最远的两个节点之间的距离。如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。
分析:把最深的左子树距离加上最深的右子树距离就是二叉树中两个节点的最大距离。
解一:GetMaxPathFromBST
/*Title: 11.求二叉树中节点的最大距离: 解一
Author: gocode
Date: 2012-10-15*/
#include <assert.h>
#include <iostream>
using namespace std;
// 二叉树结点
typedef struct tagBSTNode
{
int nValue;
tagBSTNode *psLeft;
tagBSTNode *psRight;
// 二叉树结构的构造函数,用于初始化
tagBSTNode()
{
nValue = 0;
psLeft = psRight = NULL;
}
} S_BSTNode;
// 删除结点清除内存
void DeleteAllNode(S_BSTNode* pRoot)
{
if(NULL == pRoot) return;
DeleteAllNode(pRoot->psLeft);
DeleteAllNode(pRoot->psRight);
delete pRoot;
}
// 创建二叉树
void AddBSTreeNode(S_BSTNode* &pRoot, int nValue)
{
if(NULL == pRoot)
{
pRoot = new S_BSTNode;
assert(NULL != pRoot);
pRoot->nValue = nValue;
return;
}
if(pRoot->nValue > nValue)
{
AddBSTreeNode(pRoot->psLeft, nValue);
}
else if( pRoot->nValue < nValue)
{
AddBSTreeNode(pRoot->psRight, nValue);
}
}
// 取得最大深度
int GetMaxDepth(const S_BSTNode* pRoot)
{
// 根节点为空,则返回0
if(NULL == pRoot)
return 0;
// 左右结点都为空,则返回0
// 如有一个孩子,则继续计算深度
if(NULL == pRoot->psLeft && NULL == pRoot->psRight)
return 0;
int nLeftDepth = 0, nRightDepth = 0;
if(NULL != pRoot->psLeft)
nLeftDepth = GetMaxDepth(pRoot->psLeft) + 1;
if(NULL != pRoot->psRight)
nRightDepth = GetMaxDepth(pRoot->psRight) + 1;
// 比较左右子树的深度,返回最大值
if(nRightDepth >= nLeftDepth)
return nRightDepth;
else
return nLeftDepth;
}
// 取得最大距离=左最大深度+右最大深度
int GetMaxDistance(const S_BSTNode* pRoot)
{
if(NULL == pRoot) return 0;
int nLeftDepth = GetMaxDepth(pRoot->psLeft);
nLeftDepth += ((pRoot->psLeft != NULL) ? 1 : 0);
int nRightDepth =GetMaxDepth(pRoot->psRight);
nRightDepth += ((pRoot->psRight != NULL) ? 1 : 0);
return nLeftDepth + nRightDepth;
}
int main()
{
S_BSTNode *pRoot = NULL;
AddBSTreeNode(pRoot, 5);
AddBSTreeNode(pRoot, 4);
AddBSTreeNode(pRoot, 8);
AddBSTreeNode(pRoot, 7);
AddBSTreeNode(pRoot, 12);
AddBSTreeNode(pRoot, 10);
int nMaxDis = GetMaxDistance(pRoot);
cout <<"11.求二叉树中节点的最大距离:解一"<<endl;
cout << "Max distance of Binary Search Tree is " << nMaxDis << endl;
DeleteAllNode(pRoot);
system("pause");
return 0;
}
解二:GetMaxPathFromBST2
/*Title: 11.求二叉树中节点的最大距离: 解二
Author: gocode
Date: 2012-10-15*/
#include <assert.h>
#include <iostream>
#include<tchar.h>
using namespace std;
// 数据结构定义
typedef struct NODE
{
NODE* psLeft;// 左子树
NODE* psRight;// 右子树
int nMaxLeft;// 左子树中的最长距离
int nMaxRight; // 右子树中的最长距离
int nValue;// 该节点的值
NODE()
{
nValue = nMaxLeft = nMaxRight = 0;
psLeft = psRight = NULL;
}
} S_BSTNode;
void AddBSTreeNode(S_BSTNode* &pRoot, int nValue)
{
if (NULL == pRoot)
{
pRoot = new S_BSTNode;
assert(NULL != pRoot);
pRoot->nValue = nValue;
return;
}
if (pRoot->nValue > nValue)
AddBSTreeNode(pRoot->psLeft, nValue);
else if ( pRoot->nValue < nValue)
AddBSTreeNode(pRoot->psRight, nValue);
}
void DeleteAllNode(S_BSTNode* pRoot)
{
if (NULL == pRoot)return;
DeleteAllNode(pRoot->psLeft);
DeleteAllNode(pRoot->psRight);
delete pRoot;
}
int g_nMaxLen = 0;
// 寻找树中最长的两段距离
void FindMaxLen(NODE* pRoot)
{
// 遍历到叶子节点,返回
if(NULL == pRoot)
return;
// 如果左子树为空,那么该节点的左边最长距离为0
if(NULL == pRoot->psLeft)
pRoot->nMaxLeft = 0;
// 如果右子树为空,那么该节点的右边最长距离为0
if(NULL == pRoot->psRight)
pRoot->nMaxRight = 0;
// 如果左子树不为空,递归寻找左子树最长距离
if(pRoot->psLeft != NULL)
FindMaxLen(pRoot->psLeft);
// 如果右子树不为空,递归寻找右子树最长距离
if(pRoot->psRight != NULL)
FindMaxLen(pRoot->psRight);
// 计算左子树最长节点距离
if(pRoot->psLeft != NULL)
{
int nTempMax = 0;
if(pRoot->psLeft->nMaxLeft > pRoot->psLeft->nMaxRight)
nTempMax = pRoot->psLeft->nMaxLeft;
else
nTempMax = pRoot->psLeft->nMaxRight;
pRoot->nMaxLeft = nTempMax + 1;
}
// 计算右子树最长节点距离
if(pRoot->psRight != NULL)
{
int nTempMax = 0;
if(pRoot->psRight->nMaxLeft > pRoot->psRight->nMaxRight)
nTempMax = pRoot->psRight->nMaxLeft;
else
nTempMax = pRoot->psRight->nMaxRight;
pRoot->nMaxRight = nTempMax + 1;
}
// 更新最长距离
if((pRoot->nMaxLeft + pRoot->nMaxRight) > g_nMaxLen)
g_nMaxLen = pRoot->nMaxLeft + pRoot->nMaxRight;
}
int _tmain(int argc, _TCHAR* argv[])
{
S_BSTNode *pRoot = NULL;
AddBSTreeNode(pRoot, 5);
AddBSTreeNode(pRoot, 4);
AddBSTreeNode(pRoot, 8);
AddBSTreeNode(pRoot, 7);
AddBSTreeNode(pRoot, 12);
AddBSTreeNode(pRoot, 10);
FindMaxLen(pRoot);
cout <<"11.求二叉树中节点的最大距离:解二"<<endl;
cout << "Max distance of Binary Search Tree is " << g_nMaxLen << endl;
DeleteAllNode(pRoot);
system("pause");
return 0;
}