编程之美——求二叉树中节点的最大距离
2012-12-15 09:36
239 查看
编程之美——求二叉树中节点的最大距离
1.问题
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
2.分析
书中对这个问题的分析是很清楚的,我尝试用自己的方式简短覆述。
计算一个二叉树的最大距离有两个情况:
情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。
只需要计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。
3.代码
#include<iostream>
using namespace std;
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;
}
}
void main()
{
NODE *array[8];
for(int i=0;i<8;i++)
{
array[i]=new NODE;
array[i]->chValue=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
array[i]->nMaxLeft=array[i]->nMaxRight=0;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
FindMaxLen(array[0]);
cout<<nMaxLen<<endl;
}
4.另一种解法
我认为这个问题的核心是,情况A 及 B 需要不同的信息: A 需要子树的最大深度,B 需要子树的最大距离。只要函数能在一个节点同时计算及传回这两个信息,代码就可以很简单:
struct NODE
{
NODE *pLeft;
NODE *pRight;
};
struct RESULT
{
int nMaxDistance; //节点子树的最大距离
int nMaxDepth; //节点子树的最大深度
};
RESULT GetMaximumDistance(NODE *root)
{
if(!root)
{
RESULT empty={0,-1};
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;
}
计算 result 的代码很清楚;nMaxDepth 就是左子树和右子树的深度加1;nMaxDistance 则取 A 和 B 情况的最大值。
为了减少 NULL 的条件测试,进入函数时,如果节点为 NULL,会传回一个 empty 变量。比较奇怪的是 empty.nMaxDepth = -1,目的是让调用方 +1 后,把当前的不存在的 (NULL) 子树当成最大深度为 0。
除了提高了可读性,这个解法的另一个优点是减少了 O(节点数目) 大小的侵入式资料,而改为使用 O(树的最大深度) 大小的栈空间。这个设计使函数完全没有副作用(side effect)。
参考:《编程之美》
http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html
1.问题
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
2.分析
书中对这个问题的分析是很清楚的,我尝试用自己的方式简短覆述。
计算一个二叉树的最大距离有两个情况:
情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。
只需要计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。
3.代码
#include<iostream>
using namespace std;
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;
}
}
void main()
{
NODE *array[8];
for(int i=0;i<8;i++)
{
array[i]=new NODE;
array[i]->chValue=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
array[i]->nMaxLeft=array[i]->nMaxRight=0;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
FindMaxLen(array[0]);
cout<<nMaxLen<<endl;
}
4.另一种解法
我认为这个问题的核心是,情况A 及 B 需要不同的信息: A 需要子树的最大深度,B 需要子树的最大距离。只要函数能在一个节点同时计算及传回这两个信息,代码就可以很简单:
struct NODE
{
NODE *pLeft;
NODE *pRight;
};
struct RESULT
{
int nMaxDistance; //节点子树的最大距离
int nMaxDepth; //节点子树的最大深度
};
RESULT GetMaximumDistance(NODE *root)
{
if(!root)
{
RESULT empty={0,-1};
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;
}
计算 result 的代码很清楚;nMaxDepth 就是左子树和右子树的深度加1;nMaxDistance 则取 A 和 B 情况的最大值。
为了减少 NULL 的条件测试,进入函数时,如果节点为 NULL,会传回一个 empty 变量。比较奇怪的是 empty.nMaxDepth = -1,目的是让调用方 +1 后,把当前的不存在的 (NULL) 子树当成最大深度为 0。
除了提高了可读性,这个解法的另一个优点是减少了 O(节点数目) 大小的侵入式资料,而改为使用 O(树的最大深度) 大小的栈空间。这个设计使函数完全没有副作用(side effect)。
参考:《编程之美》
http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html
相关文章推荐
- 分享:《编程之美》求二叉树中节点的最大距离
- 编程之美(二)求二叉树中两节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美--求二叉树中节点的最大距离
- 编程之美: 求二叉树中节点的最大距离》的好解法
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美之求二叉树中节点的最大距离(递归和非递归法)
- 《编程之美》读书笔记12: 3.8 求二叉树中节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美3.8 求二叉树中节点的最大距离
- 二叉树中节点的最大的距离(编程之美3.8)
- 《编程之美》读书笔记(十一):“求二叉树中节点的最大距离”扩展问题
- 《编程之美》 3.8 求二叉树中节点的最大距离
- 二叉树中节点的最大的距离(编程之美3.8)
- 编程之美之求二叉树中节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 编程之美3.8 求二叉树中节点的最大距离
- [编程之美3.8]求二叉树节点的最大距离
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法