本文转自:http://zhedahht.blog.163.com/blog/static/25411174201081263815813/
http://zhedahht.blog.163.com/blog/static/254111742008053169567/
题目:二叉树的结点定义如下:
struct TreeNode
{
int m_nvalue;
TreeNode* m_pLeft;
TreeNode* m_pRight;
};
输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。
分析:情况一:二叉树是二叉排序树,那么位于左子树的节点都比父节点小,而位于右子树的节点都比父节点大,我们只需要从树的根节点开始和两个输入的节点进行比较。如果当前节点的值比两个节点的值都大,那么最低的共同父节点一定在当前节点的左子树中,于是下一步遍历当前节点的左子结点。如果当前节点的值比两个节点的值都小,那么最低共同父节点一定在当前节点的右子树中,于是下一步遍历当前节点的右子结点。这样在树中从上到下找到的第一个在两个输入节点的值之间的节点,就是最低的公共祖先。
代码如下:
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//创建节点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = pNode->m_pRight = NULL;
return pNode;
}
//添加pLeft到pParent的左孩子
bool BinaryTreeAddLeftNode(BinaryTreeNode* pParent,BinaryTreeNode* pLeft)
{
if(NULL == pParent || NULL == pLeft)
return false;
if(pParent->m_pLeft != NULL)
{
printf("%d has left child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pLeft = pLeft;
return true;
}
}
//添加pRight到pParent的右孩子
bool BinaryTreeAddRightNode(BinaryTreeNode* pParent,BinaryTreeNode* pRight)
{
if(NULL == pParent || NULL == pRight)
return false;
if(pParent->m_pRight != NULL)
{
printf("%d has right child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pRight = pRight;
return true;
}
}
BinaryTreeNode* TheFirstCommonNode(BinaryTreeNode* pRoot,BinaryTreeNode* pNode1,BinaryTreeNode* pNode2)
{
if(NULL == pRoot || NULL == pNode1 || NULL == pNode2)
return NULL;
//树中从上到下找到的第一个在两个输入节点的值之间的节点,就是最低的公共祖先。
if(pRoot->m_nValue>pNode1->m_nValue && pRoot->m_nValue<pNode2->m_nValue)
return pRoot;
//如果当前节点的值比两个节点的值都大,那么最低的共同父节点一定在当前节点的左子树中
if(pRoot->m_nValue>pNode1->m_nValue && pRoot->m_nValue>pNode2->m_nValue)
{
if(pRoot->m_pLeft != NULL)
return TheFirstCommonNode(pRoot->m_pLeft,pNode1,pNode2);
}
//如果当前节点的值比两个节点的值都小,那么最低共同父节点一定在当前节点的右子树中
if(pRoot->m_nValue<pNode1->m_nValue && pRoot->m_nValue<pNode2->m_nValue)
{
if(pRoot->m_pRight != NULL)
return TheFirstCommonNode(pRoot->m_pRight,pNode1,pNode2);
}
}
int main()
{
//构建树
BinaryTreeNode* pRoot = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
BinaryTreeAddLeftNode(pRoot,pNode5);
BinaryTreeAddRightNode(pRoot,pNode10);
BinaryTreeAddLeftNode(pNode5,pNode4);
BinaryTreeAddRightNode(pNode5,pNode6);
BinaryTreeAddLeftNode(pNode10,pNode9);
BinaryTreeAddRightNode(pNode10,pNode11);
//测试程序
BinaryTreeNode* pNode = TheFirstCommonNode(pRoot,pNode5,pNode11);
if(NULL != pNode)
cout<<pNode->m_nValue<<endl;
}
注意在上面代码中:
TheFirstCommonNode(BinaryTreeNode* pRoot,BinaryTreeNode* pNode1,BinaryTreeNode* pNode2)函数的pNode1节点值必须小于pNode2的节点值。
如果没有指向父节点的指针的话,我们的主要问题就是怎么能得到从根节点分别到这两个节点的两条链表?
代码如下:
#include <iostream>
#include <vector>
#include <list>
#include <stdio.h>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//创建节点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = pNode->m_pRight = NULL;
return pNode;
}
//添加pLeft到pParent的左孩子
bool BinaryTreeAddLeftNode(BinaryTreeNode* pParent,BinaryTreeNode* pLeft)
{
if(NULL == pParent || NULL == pLeft)
return false;
if(pParent->m_pLeft != NULL)
{
printf("%d has left child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pLeft = pLeft;
return true;
}
}
//添加pRight到pParent的右孩子
bool BinaryTreeAddRightNode(BinaryTreeNode* pParent,BinaryTreeNode* pRight)
{
if(NULL == pParent || NULL == pRight)
return false;
if(pParent->m_pRight != NULL)
{
printf("%d has right child\n",pParent->m_nValue);
return false;
}
else
{
pParent->m_pRight = pRight;
return true;
}
}
//获得从pRoot到pNode的路径,存于path中。path保存了从pRoot到pNode的完整路径
bool GetNodePath(BinaryTreeNode* pRoot, BinaryTreeNode* pNode, std::list<BinaryTreeNode*>& path)
{
if(pRoot == pNode)
{
path.push_back(pRoot);
return true;
}
path.push_back(pRoot);
bool found = false;
if(pRoot->m_pLeft != NULL)
found = GetNodePath(pRoot->m_pLeft, pNode, path);
if(!found && pRoot->m_pRight)
found = GetNodePath(pRoot->m_pRight, pNode, path);
if(!found)
path.pop_back();
return found;
}
BinaryTreeNode* TheFirstCommonNode(BinaryTreeNode* pRoot,BinaryTreeNode* pNode1,BinaryTreeNode* pNode2)
{
if(NULL == pRoot || NULL == pNode1 || NULL == pNode2)
return NULL;
BinaryTreeNode* pLastCommanNode;
std::list<BinaryTreeNode*> path1;
std::list<BinaryTreeNode*> path2;
bool getPath1 = GetNodePath(pRoot,pNode1,path1);
bool getPath2 = GetNodePath(pRoot,pNode2,path2);
//没有得到包含pNode1和pNode2的两条路径,所以没法查找直接返回
if(false == getPath1 || false == getPath2)
return false;
std::list<BinaryTreeNode*>::const_iterator iterator1 = path1.begin();
std::list<BinaryTreeNode*>::const_iterator iterator2 = path2.begin();
//有了两条路径后,便是求两个链表的第一个相同节点的问题了
while(iterator1 != path1.end() && iterator2 != path2.end())
{
if(*iterator1 == *iterator2)
{
pLastCommanNode = *iterator1;
iterator1++;
iterator2++;
}
else
return pLastCommanNode;
}
}
int main()
{
//构建树
BinaryTreeNode* pRoot = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
BinaryTreeAddLeftNode(pRoot,pNode5);
BinaryTreeAddRightNode(pRoot,pNode10);
BinaryTreeAddLeftNode(pNode5,pNode4);
BinaryTreeAddRightNode(pNode5,pNode6);
BinaryTreeAddLeftNode(pNode10,pNode9);
BinaryTreeAddRightNode(pNode10,pNode11);
//测试程序
BinaryTreeNode* pNode = TheFirstCommonNode(pRoot,pNode6,pNode11);
if(NULL != pNode)
cout<<pNode->m_nValue<<endl;
}
下面是对如何得到两个链表的第一个公共几点的讨论:
题目:两个单向链表,找出它们的第一个公共结点。
链表的结点定义为:
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
如果两个单向链表有公共的结点,也就是说两个链表从某一结点开始,它们的m_pNext都指向同一个结点。但由于是单向链表的结点,每个结点只有一个m_pNext,因此从第一个公共结点开始,之后它们所有结点都是重合的,不可能再出现分叉。所以,两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y,而不可能像X。
看到这个题目,第一反应就是蛮力法:在第一链表上顺序遍历每个结点。每遍历一个结点的时候,在第二个链表上顺序遍历每个结点。如果此时两个链表上的结点是一样的,说明此时两个链表重合,于是找到了它们的公共结点。如果第一个链表的长度为m,第二个链表的长度为n,显然,该方法的时间复杂度为O(mn)。
接下来我们试着去寻找一个线性时间复杂度的算法。我们先把问题简化:如何判断两个单向链表有没有公共结点?前面已经提到,如果两个链表有一个公共结点,那么该公共结点之后的所有结点都是重合的。那么,它们的最后一个结点必然是重合的。因此,我们判断两个链表是不是有重合的部分,只要分别遍历两个链表到最后一个结点。如果两个尾结点是一样的,说明它们用重合;否则两个链表没有公共的结点。
在上面的思路中,顺序遍历两个链表到尾结点的时候,我们不能保证在两个链表上同时到达尾结点。这是因为两个链表不一定长度一样。但如果假设一个链表比另一个长2个结点,我们先在长的链表上遍历2个结点,这时,从长链表的当前节点开始算起,两个链表的长度是相同的,然后后再同步遍历,这个时候我们就能保证同时到达最后一个结点了。由于两个链表从第一个公共结点考试到链表的尾结点,这一部分是重合的。因此,它们肯定也是同时到达第一公共结点的。于是在遍历中,第一个相同的结点就是第一个公共的结点。
在这个思路中,我们先要分别遍历两个链表得到它们的长度,并求出两个长度之差。在长的链表上先遍历若干次之后,再同步遍历两个链表,知道找到相同的结点,或者一直到链表结束。此时,如果第一个链表的长度为m,第二个链表的长度为n,该方法的时间复杂度为O(m+n)。
代码如下:
///////////////////////////////////////////////////////////////////////
// Find the first common node in the list with head pHead1 and
// the list with head pHead2
// Input: pHead1 - the head of the first list
// pHead2 - the head of the second list
// Return: the first common node in two list. If there is no common
// nodes, return NULL
///////////////////////////////////////////////////////////////////////
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2)
{
// Get the length of two lists
unsigned int nLength1 = ListLength(pHead1);
unsigned int nLength2 = ListLength(pHead2);
int nLengthDif = nLength1 - nLength2;
// Get the longer list
ListNode *pListHeadLong = pHead1;
ListNode *pListHeadShort = pHead2;
if(nLength2 > nLength1)
{
pListHeadLong = pHead2;
pListHeadShort = pHead1;
nLengthDif = nLength2 - nLength1;
}
// Move on the longer list
for(int i = 0; i < nLengthDif; ++ i)
pListHeadLong = pListHeadLong->m_pNext;
// Move on both lists
while((pListHeadLong != NULL) &&
(pListHeadShort != NULL) &&
(pListHeadLong != pListHeadShort))
{
pListHeadLong = pListHeadLong->m_pNext;
pListHeadShort = pListHeadShort->m_pNext;
}
// Get the first common node in two lists
ListNode *pFisrtCommonNode = NULL;
if(pListHeadLong == pListHeadShort)
pFisrtCommonNode = pListHeadLong;
return pFisrtCommonNode;
}
///////////////////////////////////////////////////////////////////////
// Get the length of list with head pHead
// Input: pHead - the head of list
// Return: the length of list
///////////////////////////////////////////////////////////////////////
unsigned int ListLength(ListNode* pHead)
{
unsigned int nLength = 0;
ListNode* pNode = pHead;
while(pNode != NULL)
{
++ nLength;
pNode = pNode->m_pNext;
}
return nLength;
}
另一种解法需要借助两个栈:经过分析我们发现,如果两个链表有公共节点,那么公共节点出现在两个链表的尾部。如果我们从两个链表的尾部开始往前比较,最后一个相同的节点就是我们要找的节点。可问题是在单链表中,我们只能从头结点开始按顺序遍历,最后才能到达尾节点。最后到达的尾节点却要最先被比较,这是要用到栈的节奏啊。于是我们用栈来解决这个问题:分别把两个链表的及诶单放入两个栈里,这样两个链表的尾节点就位于两个栈的栈顶,接下来比较两个栈顶的节点是否相同。如果相同,则把栈顶弹出接着比较下一个栈顶,知道找到最后一个相同的节点。这个思路需要用到两个辅助栈。如果链表的长度分别为m和n,那么空间复杂度是O(m+n),这种思路的时间复杂度也是O(m+n),相当于使用空间换时间了。