深入二叉樹兩個結點的最低共同父結點的詳解
更新時間:2013年05月23日 18:21:23 作者:
本篇文章是對二叉樹兩個結點的最低共同父結點進行了詳細的分析介紹,需要的朋友參考下
題目:二叉樹的結點定義如下:
struct TreeNode
{
int m_nvalue;
TreeNode* m_pLeft;
TreeNode* m_pRight;
};
輸入二叉樹中的兩個結點,輸出這兩個結點在數(shù)中最低的共同父結點。
分析:求數(shù)中兩個結點的最低共同結點是面試中經常出現(xiàn)的一個問題。這個問題至少有兩個變種。
第一變種是二叉樹是一種特殊的二叉樹:查找二叉樹。也就是樹是排序過的,位于左子樹上的結點都比父結點小,而位于右子樹的結點都比父結點大。我們只需要從根結點開始和兩個結點進行比較。如果當前結點的值比兩個結點都大,則最低的共同父結點一定在當前結點的左子樹中。如果當前結點的值比兩個結點都小,則最低的共同父結點一定在當前結點的右子樹中。
第二個變種是樹不一定是二叉樹,每個結點都有一個指針指向它的父結點。于是我們可以從任何一個結點出發(fā),得到一個到達樹根結點的單向鏈表。因此這個問題轉換為求兩個單向鏈表的第一個公共結點。
現(xiàn)在我們回到這個問題本身。所謂共同的父結點,就是兩個結點都出現(xiàn)在這個結點的子樹中。因此我們可以定義一函數(shù),來判斷一個結點的子樹中是不是包含了另外一個結點。這不是件很難的事,我們可以用遞歸的方法來實現(xiàn):
/*
// If the tree with head pHead has a node pNode, return true.
// Otherwise return false.
*/
bool HasNode(TreeNode* pHead, TreeNode* pNode)
{
if(pHead == pNode)
return true;
bool has = false;
if(pHead->m_pLeft != NULL)
has = HasNode(pHead->m_pLeft, pNode);
if(!has && pHead->m_pRight != NULL)
has = HasNode(pHead->m_pRight, pNode);
return has;
}
我們可以從根結點開始,判斷以當前結點為根的樹中左右子樹是不是包含我們要找的兩個結點。如果兩個結點都出現(xiàn)在它的左子樹中,那最低的共同父結點也出現(xiàn)在它的左子樹中。如果兩個結點都出現(xiàn)在它的右子樹中,那最低的共同父結點也出現(xiàn)在它的右子樹中。如果兩個結點一個出現(xiàn)在左子樹中,一個出現(xiàn)在右子樹中,那當前的結點就是最低的共同父結點?;谶@個思路,我們可以寫出如下代碼:
/*
// Find the last parent of pNode1 and pNode2 in a tree with head pHead
*/
TreeNode* LastCommonParent_1(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)
{
if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)
return NULL;
// check whether left child has pNode1 and pNode2
bool leftHasNode1 = false;
bool leftHasNode2 = false;
if(pHead->m_pLeft != NULL)
{
leftHasNode1 = HasNode(pHead->m_pLeft, pNode1);
leftHasNode2 = HasNode(pHead->m_pLeft, pNode2);
}
if(leftHasNode1 && leftHasNode2)
{
if(pHead->m_pLeft == pNode1 || pHead->m_pLeft == pNode2)
return pHead;
return LastCommonParent_1(pHead->m_pLeft, pNode1, pNode2);
}
// check whether right child has pNode1 and pNode2
bool rightHasNode1 = false;
bool rightHasNode2 = false;
if(pHead->m_pRight != NULL)
{
if(!leftHasNode1)
rightHasNode1 = HasNode(pHead->m_pRight, pNode1);
if(!leftHasNode2)
rightHasNode2 = HasNode(pHead->m_pRight, pNode2);
}
if(rightHasNode1 && rightHasNode2)
{
if(pHead->m_pRight == pNode1 || pHead->m_pRight == pNode2)
return pHead;
return LastCommonParent_1(pHead->m_pRight, pNode1, pNode2);
}
if((leftHasNode1 && rightHasNode2) || (leftHasNode2 && rightHasNode1))
return pHead;
return NULL;
}
接著我們來分析一下這個方法的效率。函數(shù)HasNode的本質就是遍歷一棵樹,其時間復雜度是O(n)(n是樹中結點的數(shù)目)。由于我們根結點開始,要對每個結點調用函數(shù)HasNode。因此總的時間復雜度是O(n^2)。
我們仔細分析上述代碼,不難發(fā)現(xiàn)我們判斷以一個結點為根的樹是否含有某個結點時,需要遍歷樹的每個結點。接下來我們判斷左子結點或者右結點為根的樹中是否含有要找結點,仍然需要遍歷。第二次遍歷的操作其實在前面的第一次遍歷都做過了。由于存在重復的遍歷,本方法在時間效率上肯定不是最好的。
前面我們提過如果結點中有一個指向父結點的指針,我們可以把問題轉化為求兩個鏈表的共同結點?,F(xiàn)在我們可以想辦法得到這個鏈表。我們在這里稍作變化即可:
/*
// Get the path form pHead and pNode in a tree with head pHead
*/
bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::list<TreeNode*>& path)
{
if(pHead == pNode)
return true;
path.push_back(pHead);
bool found = false;
if(pHead->m_pLeft != NULL)
found = GetNodePath(pHead->m_pLeft, pNode, path);
if(!found && pHead->m_pRight)
found = GetNodePath(pHead->m_pRight, pNode, path);
if(!found)
path.pop_back();
return found;
}
由于這個路徑是從跟結點開始的。最低的共同父結點就是路徑中的最后一個共同結點:
/*
// Get the last common Node in two lists: path1 and path2
*/
TreeNode* LastCommonNode
(
const std::list<TreeNode*>& path1,
const std::list<TreeNode*>& path2
)
{
std::list<TreeNode*>::const_iterator iterator1 = path1.begin();
std::list<TreeNode*>::const_iterator iterator2 = path2.begin();
TreeNode* pLast = NULL;
while(iterator1 != path1.end() && iterator2 != path2.end())
{
if(*iterator1 == *iterator2)
pLast = *iterator1;
iterator1++;
iterator2++;
}
return pLast;
}
有了前面兩個子函數(shù)之后,求兩個結點的最低共同父結點就很容易了。我們先求出從根結點出發(fā)到兩個結點的兩條路徑,再求出兩條路徑的最后一個共同結點。代碼如下:
/*
// Find the last parent of pNode1 and pNode2 in a tree with head pHead
*/
TreeNode* LastCommonParent_2(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)
{
if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)
return NULL;
std::list<TreeNode*> path1;
GetNodePath(pHead, pNode1, path1);
std::list<TreeNode*> path2;
GetNodePath(pHead, pNode2, path2);
return LastCommonNode(path1, path2);
}
這種思路的時間復雜度是O(n),時間效率要比第一種方法好很多。但同時我們也要注意到,這種思路需要兩個鏈表來保存路徑,空間效率比不上第一個方法。
復制代碼 代碼如下:
struct TreeNode
{
int m_nvalue;
TreeNode* m_pLeft;
TreeNode* m_pRight;
};
輸入二叉樹中的兩個結點,輸出這兩個結點在數(shù)中最低的共同父結點。
分析:求數(shù)中兩個結點的最低共同結點是面試中經常出現(xiàn)的一個問題。這個問題至少有兩個變種。
第一變種是二叉樹是一種特殊的二叉樹:查找二叉樹。也就是樹是排序過的,位于左子樹上的結點都比父結點小,而位于右子樹的結點都比父結點大。我們只需要從根結點開始和兩個結點進行比較。如果當前結點的值比兩個結點都大,則最低的共同父結點一定在當前結點的左子樹中。如果當前結點的值比兩個結點都小,則最低的共同父結點一定在當前結點的右子樹中。
第二個變種是樹不一定是二叉樹,每個結點都有一個指針指向它的父結點。于是我們可以從任何一個結點出發(fā),得到一個到達樹根結點的單向鏈表。因此這個問題轉換為求兩個單向鏈表的第一個公共結點。
現(xiàn)在我們回到這個問題本身。所謂共同的父結點,就是兩個結點都出現(xiàn)在這個結點的子樹中。因此我們可以定義一函數(shù),來判斷一個結點的子樹中是不是包含了另外一個結點。這不是件很難的事,我們可以用遞歸的方法來實現(xiàn):
復制代碼 代碼如下:
/*
// If the tree with head pHead has a node pNode, return true.
// Otherwise return false.
*/
bool HasNode(TreeNode* pHead, TreeNode* pNode)
{
if(pHead == pNode)
return true;
bool has = false;
if(pHead->m_pLeft != NULL)
has = HasNode(pHead->m_pLeft, pNode);
if(!has && pHead->m_pRight != NULL)
has = HasNode(pHead->m_pRight, pNode);
return has;
}
我們可以從根結點開始,判斷以當前結點為根的樹中左右子樹是不是包含我們要找的兩個結點。如果兩個結點都出現(xiàn)在它的左子樹中,那最低的共同父結點也出現(xiàn)在它的左子樹中。如果兩個結點都出現(xiàn)在它的右子樹中,那最低的共同父結點也出現(xiàn)在它的右子樹中。如果兩個結點一個出現(xiàn)在左子樹中,一個出現(xiàn)在右子樹中,那當前的結點就是最低的共同父結點?;谶@個思路,我們可以寫出如下代碼:
復制代碼 代碼如下:
/*
// Find the last parent of pNode1 and pNode2 in a tree with head pHead
*/
TreeNode* LastCommonParent_1(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)
{
if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)
return NULL;
// check whether left child has pNode1 and pNode2
bool leftHasNode1 = false;
bool leftHasNode2 = false;
if(pHead->m_pLeft != NULL)
{
leftHasNode1 = HasNode(pHead->m_pLeft, pNode1);
leftHasNode2 = HasNode(pHead->m_pLeft, pNode2);
}
if(leftHasNode1 && leftHasNode2)
{
if(pHead->m_pLeft == pNode1 || pHead->m_pLeft == pNode2)
return pHead;
return LastCommonParent_1(pHead->m_pLeft, pNode1, pNode2);
}
// check whether right child has pNode1 and pNode2
bool rightHasNode1 = false;
bool rightHasNode2 = false;
if(pHead->m_pRight != NULL)
{
if(!leftHasNode1)
rightHasNode1 = HasNode(pHead->m_pRight, pNode1);
if(!leftHasNode2)
rightHasNode2 = HasNode(pHead->m_pRight, pNode2);
}
if(rightHasNode1 && rightHasNode2)
{
if(pHead->m_pRight == pNode1 || pHead->m_pRight == pNode2)
return pHead;
return LastCommonParent_1(pHead->m_pRight, pNode1, pNode2);
}
if((leftHasNode1 && rightHasNode2) || (leftHasNode2 && rightHasNode1))
return pHead;
return NULL;
}
接著我們來分析一下這個方法的效率。函數(shù)HasNode的本質就是遍歷一棵樹,其時間復雜度是O(n)(n是樹中結點的數(shù)目)。由于我們根結點開始,要對每個結點調用函數(shù)HasNode。因此總的時間復雜度是O(n^2)。
我們仔細分析上述代碼,不難發(fā)現(xiàn)我們判斷以一個結點為根的樹是否含有某個結點時,需要遍歷樹的每個結點。接下來我們判斷左子結點或者右結點為根的樹中是否含有要找結點,仍然需要遍歷。第二次遍歷的操作其實在前面的第一次遍歷都做過了。由于存在重復的遍歷,本方法在時間效率上肯定不是最好的。
前面我們提過如果結點中有一個指向父結點的指針,我們可以把問題轉化為求兩個鏈表的共同結點?,F(xiàn)在我們可以想辦法得到這個鏈表。我們在這里稍作變化即可:
復制代碼 代碼如下:
/*
// Get the path form pHead and pNode in a tree with head pHead
*/
bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::list<TreeNode*>& path)
{
if(pHead == pNode)
return true;
path.push_back(pHead);
bool found = false;
if(pHead->m_pLeft != NULL)
found = GetNodePath(pHead->m_pLeft, pNode, path);
if(!found && pHead->m_pRight)
found = GetNodePath(pHead->m_pRight, pNode, path);
if(!found)
path.pop_back();
return found;
}
由于這個路徑是從跟結點開始的。最低的共同父結點就是路徑中的最后一個共同結點:
復制代碼 代碼如下:
/*
// Get the last common Node in two lists: path1 and path2
*/
TreeNode* LastCommonNode
(
const std::list<TreeNode*>& path1,
const std::list<TreeNode*>& path2
)
{
std::list<TreeNode*>::const_iterator iterator1 = path1.begin();
std::list<TreeNode*>::const_iterator iterator2 = path2.begin();
TreeNode* pLast = NULL;
while(iterator1 != path1.end() && iterator2 != path2.end())
{
if(*iterator1 == *iterator2)
pLast = *iterator1;
iterator1++;
iterator2++;
}
return pLast;
}
有了前面兩個子函數(shù)之后,求兩個結點的最低共同父結點就很容易了。我們先求出從根結點出發(fā)到兩個結點的兩條路徑,再求出兩條路徑的最后一個共同結點。代碼如下:
復制代碼 代碼如下:
/*
// Find the last parent of pNode1 and pNode2 in a tree with head pHead
*/
TreeNode* LastCommonParent_2(TreeNode* pHead, TreeNode* pNode1, TreeNode* pNode2)
{
if(pHead == NULL || pNode1 == NULL || pNode2 == NULL)
return NULL;
std::list<TreeNode*> path1;
GetNodePath(pHead, pNode1, path1);
std::list<TreeNode*> path2;
GetNodePath(pHead, pNode2, path2);
return LastCommonNode(path1, path2);
}
這種思路的時間復雜度是O(n),時間效率要比第一種方法好很多。但同時我們也要注意到,這種思路需要兩個鏈表來保存路徑,空間效率比不上第一個方法。
您可能感興趣的文章:
- 平衡二叉樹AVL操作模板
- 平衡二叉樹的實現(xiàn)實例
- 二叉樹的非遞歸后序遍歷算法實例詳解
- 二叉樹先序遍歷的非遞歸算法具體實現(xiàn)
- 二叉樹先根(先序)遍歷的改進
- c語言版本二叉樹基本操作示例(先序 遞歸 非遞歸)
- python二叉樹遍歷的實現(xiàn)方法
- python二叉樹的實現(xiàn)實例
- C++二叉樹結構的建立與基本操作
- 二叉樹遍歷 非遞歸 C++實現(xiàn)代碼
- 先序遍歷二叉樹的遞歸實現(xiàn)與非遞歸實現(xiàn)深入解析
- PHP Class&Object -- 解析PHP實現(xiàn)二叉樹
- PHP Class&Object -- PHP 自排序二叉樹的深入解析
- 如何在二叉樹中找出和為某一值的所有路徑
- 探討:C++實現(xiàn)鏈式二叉樹(用非遞歸方式先序,中序,后序遍歷二叉樹)
- 深入理解二叉樹的非遞歸遍歷
- 深入遍歷二叉樹的各種操作詳解(非遞歸遍歷)
- 二叉樹前序遍歷的非遞歸算法
相關文章
教你使用Matlab制作圖形驗證碼生成器(app designer)
這篇文章主要和大家分享如何利用Matlab制作一款圖形驗證碼生成器,文中的實現(xiàn)步驟講解詳細,感興趣的小伙伴可以跟隨小編動手試一試2022-02-02
C++多態(tài)與虛擬之C++編譯器對函數(shù)名的改編(Name?Mangling)
在Windows?DLLs中,使用C++編寫的DllMain()等callback函數(shù)需避免C++編譯器進行name?mangling,因此需使用extern?"C",這篇文章主要介紹了C++多態(tài)與虛擬:C++編譯器對函數(shù)名的改編(Name?Mangling),需要的朋友可以參考下2024-04-04

