代碼來源與《劍指offer》ios
獲得從根節點開始到輸入的兩個結點的兩條,須要遍歷兩次樹,每遍歷一次的時間複雜度是O(n),獲得的兩條路徑的長度在最差狀況時是O(n),一般狀況下兩條路徑的長度是O(logn)。spa
#include <iostream> #include <vector> #include <list> using namespace std; struct TreeNode { int m_nValue; std::vector<TreeNode*> m_vChildren; }; bool GetNodePath(TreeNode *pRoot,TreeNode *pNode, list<TreeNode*> &path) { if (pRoot=pNode) { return true; } path.push_back(pRoot); bool found=false; vector<TreeNode*>::iterator i=pRoot->m_vChildren.begin(); while(!found&&i<pRoot->m_vChildren.end()) { found=GetNodePath(*i,pNode,path); ++i; } if (!found) { path.pop_back(); } return found; } TreeNode* GetLastCommonNode(const list<TreeNode*>& path1, const list<TreeNode*>& path2) { list<TreeNode*>::const_iterator i1=path1.begin(); list<TreeNode*>::const_iterator i2=path2.begin(); TreeNode* pLast=NULL; while(i1!=path1.end()&&i2!=path2.end()) { if (*i1==*i2) { pLast=*iterator; } i1++; i2++; } return pLast; } TreeNode* GetLastCommonParent(TreeNode* pRoot,TreeNode* pNode1,TreeNode* pNode2) { if (pRoot==NULL||pNode1==NULL||pNode2==NULL) { return NULL; } list<TreeNode*> path1; GetNodePath(pRoot,pNode1,path1); list<TreeNode*> path2; GetNodePath(pRoot,pNode2,path2); return GetLastCommonNode(path1,path2); }