c++实现二叉树中节点的最大距离

来源:互联网 发布:2016年美国非农数据 编辑:程序博客网 时间:2024/06/05 01:14

文章前半部分能懂,可是后面的Milo不是很理解,可能有待以后学习.....

微软面试题之一,难度系数中,题目描述如下:

求二叉树中节点的最大距离... 
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的, 
我们姑且定义"距离"为两节点之间边的个数。 
写一个程序, 
求一棵二叉树中相距最远的两个节点之间的距离。 

 

逻辑分析:

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为边的集合。


下面给出编程之美上的代码

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3.   
  4. struct NODE  
  5. {  
  6.     NODE *pLeft;  
  7.     NODE *pRight;  
  8.     int nMaxLeft;  
  9.     int nMaxRight;  
  10.     char chValue;  
  11. };  
  12.   
  13. int nMaxLen = 0;  
  14.   
  15. void FindMaxLen(NODE* root)  
  16. {  
  17.     //递归结束   
  18.     if(root==NULL)    return;  
  19.       
  20.     //左树为空   
  21.     if(root->pLeft==NULL)  
  22.         root->nMaxLeft=0;  
  23.       
  24.     //右树为空   
  25.     if(root->pRight==NULL)  
  26.         root->pRight=0;  
  27.       
  28.     //左树不为空   
  29.     if(root->pLeft!=NULL)  
  30.     {  
  31.         FindMaxLen(root->pLeft);  
  32.     }  
  33.       
  34.     //右树不为空   
  35.     if(root->pRight!=NULL)  
  36.     {  
  37.         FindMaxLen(root->pRight);  
  38.     }  
  39.       
  40.     //求左子树最大距离   
  41.     if(root->pLeft!=NULL)  
  42.     {  
  43.         int nTempMax=0;  
  44.         if(root->pLeft->nMaxLeft>root->pLeft->nMaxRight)  
  45.             nTempMax=root->pLeft->nMaxLeft;  
  46.         else  
  47.             nTempMax=root->pLeft->nMaxRight;  
  48.         root->nMaxLeft=nTempMax+1;  
  49.     }  
  50.       
  51.     //求右子树最大距离   
  52.     if(root->pRight!=NULL)  
  53.     {  
  54.         int nTempMax=0;  
  55.         if(root->pRight->nMaxLeft>root->pRight->nMaxRight)  
  56.             nTempMax=root->pRight->nMaxLeft;  
  57.         else  
  58.             nTempMax=root->pRight->nMaxRight;  
  59.         root->nMaxRight=nTempMax+1;  
  60.     }  
  61.       
  62.     //更新最大距离   
  63.     if(root->nMaxLeft+root->nMaxRight>nMaxLen)  
  64.         nMaxLen=root->nMaxLeft+root->nMaxRight;  
  65. }  
  66.   
  67. NODE* InitTree()  
  68. {  
  69.     NODE* tree[10];  
  70.       
  71.     for(int i=0;i<10;i++)  
  72.     {  
  73.         tree[i]=(NODE*)malloc(sizeof(NODE));  
  74.         tree[i]->nMaxLeft=0;  
  75.         tree[i]->nMaxRight=0;  
  76.         tree[i]->pLeft=NULL;  
  77.         tree[i]->pRight=NULL;  
  78.         tree[i]->chValue=(char)i;  
  79.     }  
  80.     for(i=0;i<=2;i++)  
  81.     {  
  82.         tree[i]->pLeft=tree[2*i+1];  
  83.         tree[i]->pRight=tree[2*i+2];  
  84.     }  
  85.     tree[3]->pLeft=tree[7];  
  86.     tree[5]->pRight=tree[8];  
  87.     return tree[0];  
  88. }  
  89.   
  90. int main()  
  91. {  
  92.     FindMaxLen(InitTree());  
  93.     printf("%d\n",nMaxLen);  
  94.     return 0;  
  95. }  



5、很明显,nMaxLeft和nMaxRight在完成我们DP的记账本同时,破坏了二叉树的结构,这将给后者的应用性带来非常大的负担,逻辑上相对复杂,Milo给出了更为简洁的,DP版本。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>   
  2.    
  3. using namespace std;  
  4.    
  5. struct NODE  
  6. {  
  7.     NODE *pLeft;  
  8.     NODE *pRight;  
  9. };  
  10.    
  11. struct RESULT  
  12. {  
  13.     int nMaxDistance;  
  14.     int nMaxDepth;  
  15. };  
  16.    
  17. RESULT GetMaximumDistance(NODE* root)  
  18. {  
  19.     if (!root)  
  20.     {  
  21.         RESULT empty = { 0, -1 };   // trick: nMaxDepth is -1 and then caller will plus 1 to balance it as zero.  
  22.         return empty;  
  23.     }  
  24.    
  25.     RESULT lhs = GetMaximumDistance(root->pLeft);  
  26.     RESULT rhs = GetMaximumDistance(root->pRight);  
  27.    
  28.     RESULT result;  
  29.     result.nMaxDepth = max(lhs.nMaxDepth + 1, rhs.nMaxDepth + 1);  
  30.     result.nMaxDistance = max(max(lhs.nMaxDistance, rhs.nMaxDistance), lhs.nMaxDepth + rhs.nMaxDepth + 2);  
  31.     return result;  
  32. }  

这里借用Milo的自荐:

计算 result 的代码很清楚;nMaxDepth 就是左子树和右子树的深度加1;nMaxDistance 则取 A 和 B 情况的最大值。

为了减少 NULL 的条件测试,进入函数时,如果节点为 NULL,会传回一个 empty 变量。比较奇怪的是 empty.nMaxDepth = -1,目的是让调用方 +1 后,把当前的不存在的 (NULL) 子树当成最大深度为 0。

除了提高了可读性,这个解法的另一个优点是减少了 O(节点数目) 大小的侵入式资料,而改为使用 O(树的最大深度) 大小的栈空间。这个设计使函数完全没有副作用(side effect)。

测试代码:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void Link(NODE* nodes, int parent, int left, int right)  
  2. {  
  3.     if (left != -1)  
  4.         nodes[parent].pLeft = &nodes[left];   
  5.    
  6.     if (right != -1)  
  7.         nodes[parent].pRight = &nodes[right];  
  8. }  
  9.    
  10. void main()  
  11. {  
  12.     // P. 241 Graph 3-12   
  13.     NODE test1[9] = { 0 };  
  14.     Link(test1, 0, 1, 2);  
  15.     Link(test1, 1, 3, 4);  
  16.     Link(test1, 2, 5, 6);  
  17.     Link(test1, 3, 7, -1);  
  18.     Link(test1, 5, -1, 8);  
  19.     cout << "test1: " << GetMaximumDistance(&test1[0]).nMaxDistance << endl;  
  20.    
  21.     // P. 242 Graph 3-13 left   
  22.     NODE test2[4] = { 0 };  
  23.     Link(test2, 0, 1, 2);  
  24.     Link(test2, 1, 3, -1);  
  25.     cout << "test2: " << GetMaximumDistance(&test2[0]).nMaxDistance << endl;  
  26.    
  27.     // P. 242 Graph 3-13 right   
  28.     NODE test3[9] = { 0 };  
  29.     Link(test3, 0, -1, 1);  
  30.     Link(test3, 1, 2, 3);  
  31.     Link(test3, 2, 4, -1);  
  32.     Link(test3, 3, 5, 6);  
  33.     Link(test3, 4, 7, -1);  
  34.     Link(test3, 5, -1, 8);  
  35.     cout << "test3: " << GetMaximumDistance(&test3[0]).nMaxDistance << endl;  
  36.    
  37.     // P. 242 Graph 3-14   
  38.     // Same as Graph 3-2, not test   
  39.    
  40.     // P. 243 Graph 3-15   
  41.     NODE test4[9] = { 0 };  
  42.     Link(test4, 0, 1, 2);  
  43.     Link(test4, 1, 3, 4);  
  44.     Link(test4, 3, 5, 6);  
  45.     Link(test4, 5, 7, -1);  
  46.     Link(test4, 6, -1, 8);  
  47.     cout << "test4: " << GetMaximumDistance(&test4[0]).nMaxDistance << endl;  
  48. }  


1 0