微软面试题之一,难度系数中,题目描述如下:
求二叉树中节点的最大距离...
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,
我们姑且定义"距离"为两节点之间边的个数。
写一个程序,
求一棵二叉树中相距最远的两个节点之间的距离。
逻辑分析:
1、看到这道题的时候,很容易产生一种错觉,这题不就是求二叉树高度吗。。。显然,出题人不是小白,所以这里面一定有文章,继而注意到“双向”,玄机也正在此处。两张图说明一切,不解释。
2、题意清楚了,下一步就是分析如何去做,我们都知道,但凡是树的题目,一般都要考虑递归解。而针对这道题,我们通过分析可以得到几个比较显眼的结论:最长的路径一定包含叶子;最长的路径有两种情况,含有根节点,从左子树最深节点,到右子树最深节点,或者是不含根节点,而是左子树或者右子树的最长路径,递归而下,也就是上图的A,B。
3、总结上述说法,即是相距最远的两个节点,一定是两个叶子节点,或者一个叶子节点到根节点。对于任意一个节点,以该节点为根R,假设该节点有k个孩子节点,那么相距最远的两个节点U,V要么经过R(U、V也就是不同子树上的最深节点),要么不经过R,而是根的某一棵子树,且U、V一定也是子树的左右子树最深节点。
4、显然,我们自上而下的,采用类似深度遍历的方法递归下去,而又采用自底向上的动态规划思想,加以解决。
设第K棵子树中最远的两个节点Uk和Vk,其距离定义为d(Uk,Vk),那么节点Uk或Vk即为子树K到根节点Rk距离最长的节点。不失一般性,我们设Uk为子树K中到根节点Rk距离最长的节点,其到根节点的距离定义为d(Uk,R)。取d(Ui,R)(1<=i<=k)中最大的两个值max1和max2,那么经过根节点R的最长路径为max1+max2+2,所以树R中相距最远的两个点的距离为:max{d(U1,V1),…, d(Uk,Vk),max1+max2+2}。而采用深度遍历的模型,我们知道,每一个节点都只遍历一次,且所有节点都需要经过遍历,那么时间复杂度为O(|E|)=O(|V|-1),其中V为点的集合,E为边的集合。
下面给出编程之美上的代码
#include <stdio.h>
#include <stdlib.h>
struct NODE
{
NODE *pLeft;
NODE *pRight;
int nMaxLeft;
int nMaxRight;
char chValue;
};
int nMaxLen = 0;
void FindMaxLen(NODE* root)
{
//递归结束
if(root==NULL) return;
//左树为空
if(root->pLeft==NULL)
root->nMaxLeft=0;
//右树为空
if(root->pRight==NULL)
root->pRight=0;
//左树不为空
if(root->pLeft!=NULL)
{
FindMaxLen(root->pLeft);
}
//右树不为空
if(root->pRight!=NULL)
{
FindMaxLen(root->pRight);
}
//求左子树最大距离
if(root->pLeft!=NULL)
{
int nTempMax=0;
if(root->pLeft->nMaxLeft>root->pLeft->nMaxRight)
nTempMax=root->pLeft->nMaxLeft;
else
nTempMax=root->pLeft->nMaxRight;
root->nMaxLeft=nTempMax+1;
}
//求右子树最大距离
if(root->pRight!=NULL)
{
int nTempMax=0;
if(root->pRight->nMaxLeft>root->pRight->nMaxRight)
nTempMax=root->pRight->nMaxLeft;
else
nTempMax=root->pRight->nMaxRight;
root->nMaxRight=nTempMax+1;
}
//更新最大距离
if(root->nMaxLeft+root->nMaxRight>nMaxLen)
nMaxLen=root->nMaxLeft+root->nMaxRight;
}
NODE* InitTree()
{
NODE* tree[10];
for(int i=0;i<10;i++)
{
tree[i]=(NODE*)malloc(sizeof(NODE));
tree[i]->nMaxLeft=0;
tree[i]->nMaxRight=0;
tree[i]->pLeft=NULL;
tree[i]->pRight=NULL;
tree[i]->chValue=(char)i;
}
for(i=0;i<=2;i++)
{
tree[i]->pLeft=tree[2*i+1];
tree[i]->pRight=tree[2*i+2];
}
tree[3]->pLeft=tree[7];
tree[5]->pRight=tree[8];
return tree[0];
}
int main()
{
FindMaxLen(InitTree());
printf("%d\n",nMaxLen);
return 0;
}
5、很明显,nMaxLeft和nMaxRight在完成我们DP的记账本同时,破坏了二叉树的结构,这将给后者的应用性带来非常大的负担,逻辑上相对复杂,Milo给出了更为简洁的,DP版本。
#include <iostream>
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;
}
这里借用Milo的自荐:
计算 result 的代码很清楚;nMaxDepth 就是左子树和右子树的深度加1;nMaxDistance 则取 A 和 B 情况的最大值。
为了减少 NULL 的条件测试,进入函数时,如果节点为 NULL,会传回一个 empty 变量。比较奇怪的是 empty.nMaxDepth = -1,目的是让调用方 +1 后,把当前的不存在的 (NULL) 子树当成最大深度为 0。
除了提高了可读性,这个解法的另一个优点是减少了 O(节点数目) 大小的侵入式资料,而改为使用 O(树的最大深度) 大小的栈空间。这个设计使函数完全没有副作用(side effect)。
测试代码:
void Link(NODE* nodes, int parent, int left, int right)
{
if (left != -1)
nodes[parent].pLeft = &nodes[left];
if (right != -1)
nodes[parent].pRight = &nodes[right];
}
void main()
{
// P. 241 Graph 3-12
NODE test1[9] = { 0 };
Link(test1, 0, 1, 2);
Link(test1, 1, 3, 4);
Link(test1, 2, 5, 6);
Link(test1, 3, 7, -1);
Link(test1, 5, -1, 8);
cout << "test1: " << GetMaximumDistance(&test1[0]).nMaxDistance << endl;
// P. 242 Graph 3-13 left
NODE test2[4] = { 0 };
Link(test2, 0, 1, 2);
Link(test2, 1, 3, -1);
cout << "test2: " << GetMaximumDistance(&test2[0]).nMaxDistance << endl;
// P. 242 Graph 3-13 right
NODE test3[9] = { 0 };
Link(test3, 0, -1, 1);
Link(test3, 1, 2, 3);
Link(test3, 2, 4, -1);
Link(test3, 3, 5, 6);
Link(test3, 4, 7, -1);
Link(test3, 5, -1, 8);
cout << "test3: " << GetMaximumDistance(&test3[0]).nMaxDistance << endl;
// P. 242 Graph 3-14
// Same as Graph 3-2, not test
// P. 243 Graph 3-15
NODE test4[9] = { 0 };
Link(test4, 0, 1, 2);
Link(test4, 1, 3, 4);
Link(test4, 3, 5, 6);
Link(test4, 5, 7, -1);
Link(test4, 6, -1, 8);
cout << "test4: " << GetMaximumDistance(&test4[0]).nMaxDistance << endl;
}