/*
*1.编程之美上的解法。
*/
struct NODE
{
NODE* pLeft; // 左子树
NODE* pRight; // 右子树
int nMaxLeft; // 左子树中的最长距离
int nMaxRight; // 右子树中的最长距离
char chValue; // 该节点的值
};
int nMaxLen = 0;
// 寻找树中最长的两段距离
void FindMaxLen(NODE* pRoot)
{
// 遍历到叶子节点,返回
if(pRoot == NULL)
{
return;
}
// 如果左子树为空,那么该节点的左边最长距离为0
if(pRoot -> pLeft == NULL)
{
pRoot -> nMaxLeft = 0;
}
// 如果右子树为空,那么该节点的右边最长距离为0
if(pRoot -> pRight == NULL)
{
pRoot -> nMaxRight = 0;
}
// 如果左子树不为空,递归寻找左子树最长距离
if(pRoot -> pLeft != NULL)
{
FindMaxLen(pRoot -> pLeft);
}
// 如果右子树不为空,递归寻找右子树最长距离
if(pRoot -> pRight != NULL)
{
FindMaxLen(pRoot -> pRight);
}
// 计算左子树最长节点距离
if(pRoot -> pLeft != NULL)
{
int nTempMax = 0;
if(pRoot -> pLeft -> nMaxLeft > pRoot -> pLeft -> nMaxRight)
{
nTempMax = pRoot -> pLeft -> nMaxLeft;
}
else
{
nTempMax = pRoot -> pLeft -> nMaxRight;
}
pRoot -> nMaxLeft = nTempMax + 1;
}
// 计算右子树最长节点距离
if(pRoot -> pRight != NULL)
{
int nTempMax = 0;
if(pRoot -> pRight -> nMaxLeft > pRoot -> pRight -> nMaxRight)
{
nTempMax = pRoot -> pRight -> nMaxLeft;
}
else
{
nTempMax = pRoot -> pRight -> nMaxRight;
}
pRoot -> nMaxRight = nTempMax + 1;
}
// 更新最长距离
if(pRoot -> nMaxLeft + pRoot -> nMaxRight > nMaxLen)
{
nMaxLen = pRoot -> nMaxLeft + pRoot -> nMaxRight;
}
}
//其他解法2
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEAF -1
typedef struct BTreeNode{
BTreeNode* lchild;
BTreeNode* rchild;
int value;
}BTreeNode,*Btree;
BTreeNode* createTree(){
BTreeNode* T;
int t;
scanf("%d",&t);
if(t == LEAF){
T = NULL;
}else{
T = (BTreeNode *) malloc(sizeof(BTreeNode));
T->value = t;
T->lchild = createTree();
T->rchild = createTree();
}
return T;
}
int max(int a,int b){
return a > b ? a : b;
}
int findMaxLen(BTreeNode* root,int &depth){
if(root == NULL){
depth = 0;
return 0;
}
int leftLen = 0,rightLen = 0,maxLeft = 0,maxRight = 0;
if(root->lchild != NULL){
maxLeft = findMaxLen(root->lchild,leftLen);
}
if(root->rchild != NULL){
maxRight = findMaxLen(root->rchild,rightLen);
}
depth = max(leftLen,rightLen) + 1;
return max(maxLeft,max(maxRight,leftLen + rightLen));
}
main(){
BTreeNode * root;
root = createTree();
int depth = 0,maxLen = 0;
maxLen = findMaxLen(root,depth);
printf("%d \n",maxLen);
system("pause");
return 0;
}
//引用:http://www.cnblogs.com/miloyip/archive/2010/02/25/binary_tree_distance.html
//解法3
using namespace std;
struct NODE
{
NODE *pLeft;
NODE *pRight;
};
struct RESULT
{
int nMaxDistance;
int nMaxDepth;
};
RESULT GetMaximumDistance(NODE* root)
{
if (!root)
{
RESULT empty = { 0, -1 }; // trick: nMaxDepth is -1 and then caller will plus 1 to balance it as zero.
return empty;
}
RESULT lhs = GetMaximumDistance(root->pLeft);
RESULT rhs = GetMaximumDistance(root->pRight);
RESULT result;
result.nMaxDepth = max(lhs.nMaxDepth + 1, rhs.nMaxDepth + 1);
result.nMaxDistance = max(max(lhs.nMaxDistance, rhs.nMaxDistance), lhs.nMaxDepth + rhs.nMaxDepth + 2);
return result;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- howto234.com 版权所有 湘ICP备2022005869号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务