【转】最近公共祖先问题

题目:二叉树的结点定义如下:

struct TreeNode

{

    int m_nvalue;

    TreeNode* m_pLeft;

    TreeNode* m_pRight;

};

输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。

分析:求数中两个结点的最低共同结点是面试中经常出现的一个问题。这个问题至少有两个变种。

第一变种是二叉树是一种特殊的二叉树:查找二叉树。也就是树是排序过的,位于左子树上的结点都比父结点小,而位于右子树的结点都比父结点大。我们只需要从根结点开始和两个结点进行比较。如果当前结点的值比两个结点都大,则最低的共同父结点一定在当前结点的左子树中。如果当前结点的值比两个结点都小,则最低的共同父结点一定在当前结点的右子树中。

第二个变种是树不一定是二叉树,每个结点都有一个指针指向它的父结点。于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表。因此这个问题转换为两个单向链表的第一个公共结点

现在我们回到这个问题本身。所谓共同的父结点,就是两个结点都出现在这个结点的子树中。因此我们可以定义一函数,来判断一个结点的子树中是不是包含了另外一个结点。这不是件很难的事,我们可以用递归的方法来实现:

  1. /   
  2.   
  3. // If the tree with head pHead has a node pNode, return true.   
  4.   
  5. // Otherwise return false.   
  6.   
  7. /   
  8.   
  9. bool HasNode(TreeNode* pHead, TreeNode* pNode)  
  10.   
  11. {  
  12.   
  13.     if(pHead == pNode)  
  14.   
  15.         return true;  
  16.   
  17.    
  18.   
  19.     bool has = false;  
  20.   
  21.    
  22.   
  23.     if(pHead->m_pLeft != NULL)  
  24.   
  25.         has = HasNode(pHead->m_pLeft, pNode);  
  26.   
  27.    
  28.   
  29.     if(!has && pHead->m_pRight != NULL)  
  30.   
  31.         has = HasNode(pHead->m_pRight, pNode);  
  32.   
  33.    
  34.   
  35.     return has;  
  36.   
  37. }  

 

我们可以从根结点开始,判断以当前结点为根的树中左右子树是不是包含我们要找的两个结点。如果两个结点都出现在它的左子树中,那最低的共同父结点也出现在它的左子树中。如果两个结点都出现在它的右子树中,那最低的共同父结点也出现在它的右子树中。如果两个结点一个出现在左子树中,一个出现在右子树中,那当前的结点就是最低的共同父结点。基于这个思路,我们可以写出如下代码:

  1. /   
  2.   
  3. // Find the last parent of pNode1 and pNode2 in a tree with head pHead   
  4.   
  5. /   
  6.   
  7. TreeNode* LastCommonParent_1(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)  
  8.   
  9. {  
  10.   
  11.     if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)  
  12.   
  13.         return NULL;  
  14.   
  15.    
  16.   
  17.     // check whether left child has pNode1 and pNode2   
  18.   
  19.     bool leftHasNode1 = false;  
  20.   
  21.     bool leftHasNode2 = false;  
  22.   
  23.     if(pHead->m_pLeft != NULL)  
  24.   
  25.     {  
  26.   
  27.         leftHasNode1 = HasNode(pHead->m_pLeft, pNode1);  
  28.   
  29.         leftHasNode2 = HasNode(pHead->m_pLeft, pNode2);  
  30.   
  31.     }  
  32.   
  33.    
  34.   
  35.     if(leftHasNode1 && leftHasNode2)  
  36.   
  37.     {  
  38.   
  39.         if(pHead->m_pLeft == pNode1 || pHead->m_pLeft == pNode2)  
  40.   
  41.             return pHead;  
  42.   
  43.    
  44.   
  45.         return LastCommonParent_1(pHead->m_pLeft, pNode1, pNode2);  
  46.   
  47.     }  
  48.   
  49.    
  50.   
  51.     // check whether right child has pNode1 and pNode2   
  52.   
  53.     bool rightHasNode1 = false;  
  54.   
  55.     bool rightHasNode2 = false;  
  56.   
  57.     if(pHead->m_pRight != NULL)  
  58.   
  59.     {  
  60.   
  61.         if(!leftHasNode1)  
  62.   
  63.             rightHasNode1 = HasNode(pHead->m_pRight, pNode1);  
  64.   
  65.         if(!leftHasNode2)  
  66.   
  67.             rightHasNode2 = HasNode(pHead->m_pRight, pNode2);  
  68.   
  69.     }  
  70.   
  71.    
  72.   
  73.     if(rightHasNode1 && rightHasNode2)  
  74.   
  75.     {  
  76.   
  77.         if(pHead->m_pRight == pNode1 || pHead->m_pRight == pNode2)  
  78.   
  79.             return pHead;  
  80.   
  81.    
  82.   
  83.         return LastCommonParent_1(pHead->m_pRight, pNode1, pNode2);  
  84.   
  85.     }  
  86.   
  87.    
  88.   
  89.     if((leftHasNode1 && rightHasNode2)  
  90.   
  91.         || (leftHasNode2 && rightHasNode1))  
  92.   
  93.         return pHead;  
  94.   
  95.    
  96.   
  97.     return NULL;  
  98.   
  99. }  

接着我们来分析一下这个方法的效率。函数HasNode的本质就是遍历一棵树,其时间复杂度是O(n)(n是树中结点的数目)。由于我们根结点开始,要对每个结点调用函数HasNode。因此总的时间复杂度是O(n2)。

我们仔细分析上述代码,不难发现我们判断以一个结点为根的树是否含有某个结点时,需要遍历树的每个结点。接下来我们判断左子结点或者右结点为根的树中是否含有要找结点,仍然需要遍历。第二次遍历的操作其实在前面的第一次遍历都做过了。由于存在重复的遍历,本方法在时间效率上肯定不是最好的。

前面我们提过如果结点中有一个指向父结点的指针,我们可以把问题转化为求两个链表的共同结点。现在我们可以想办法得到这个链表。我们在中分析过如何得到一条从根结点开始的路径。我们在这里稍作变化即可:

 

  1. /   
  2.   
  3. // Get the path form pHead and pNode in a tree with head pHead   
  4.   
  5. /   
  6.   
  7. bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::list<TreeNode*>& path)  
  8.   
  9. {  
  10.   
  11.     if(pHead == pNode)  
  12.   
  13.         return true;  
  14.   
  15.    
  16.   
  17.     path.push_back(pHead);  
  18.   
  19.    
  20.   
  21.     bool found = false;  
  22.   
  23.     if(pHead->m_pLeft != NULL)  
  24.   
  25.         found = GetNodePath(pHead->m_pLeft, pNode, path);  
  26.   
  27.     if(!found && pHead->m_pRight)  
  28.   
  29.         found = GetNodePath(pHead->m_pRight, pNode, path);  
  30.   
  31.    
  32.   
  33.     if(!found)  
  34.   
  35.         path.pop_back();  
  36.   
  37.    
  38.   
  39.     return found;  
  40.   
  41. }  
  42.   
  43. 由于这个路径是从跟结点开始的。最低的共同父结点就是路径中的最后一个共同结点:  
  44.   
  45. /   
  46.   
  47. // Get the last common Node in two lists: path1 and path2   
  48.   
  49. /   
  50.   
  51. TreeNode* LastCommonNode  
  52.   
  53. (  
  54.   
  55.     const std::list<TreeNode*>& path1,   
  56.   
  57.     const std::list<TreeNode*>& path2  
  58.   
  59. )  
  60.   
  61. {  
  62.   
  63.     std::list<TreeNode*>::const_iterator iterator1 = path1.begin();  
  64.   
  65.     std::list<TreeNode*>::const_iterator iterator2 = path2.begin();  
  66.   
  67.       
  68.   
  69.     TreeNode* pLast = NULL;  
  70.   
  71.    
  72.   
  73.     while(iterator1 != path1.end() && iterator2 != path2.end())  
  74.   
  75.     {  
  76.   
  77.         if(*iterator1 == *iterator2)  
  78.   
  79.             pLast = *iterator1;  
  80.   
  81.    
  82.   
  83.         iterator1++;  
  84.   
  85.         iterator2++;  
  86.   
  87.     }  
  88.   
  89.    
  90.   
  91.     return pLast;  
  92.   
  93. }  
  94.   
  95. 有了前面两个子函数之后,求两个结点的最低共同父结点就很容易了。我们先求出从根结点出发到两个结点的两条路径,再求出两条路径的最后一个共同结点。代码如下:  
  96.   
  97. /   
  98.   
  99. // Find the last parent of pNode1 and pNode2 in a tree with head pHead   
  100.   
  101. /   
  102.   
  103. TreeNode* LastCommonParent_2(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)  
  104.   
  105. {  
  106.   
  107.     if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)  
  108.   
  109.         return NULL;  
  110.   
  111.    
  112.   
  113.     std::list<TreeNode*> path1;  
  114.   
  115.     GetNodePath(pHead, pNode1, path1);  
  116.   
  117.    
  118.   
  119.     std::list<TreeNode*> path2;  
  120.   
  121.     GetNodePath(pHead, pNode2, path2);  
  122.   
  123.    
  124.   
  125.     return LastCommonNode(path1, path2);  
  126.   
  127. }  
  128.   
  129. 这种思路的时间复杂度是O(n),时间效率要比第一种方法好很多。但同时我们也要注意到,这种思路需要两个链表来保存路径,空间效率比不上第一个方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值