求一棵普通树的两个结点的最低公共祖先

一棵普通树,树中的结点没有指向父节点的指针,求一棵普通树的两个结点的最低公共祖先。

代码如下,我太懒没有加注释,大家自己看吧!

  1 #include <iostream>

  2 #include <list>

  3 #include <vector>

  4 using namespace std;

  5 

  6 struct TreeNode //节点

  7 {

  8     char m_nValue;

  9     vector<TreeNode*> m_vChildren;

 10 };

 11 

 12 TreeNode* ConstructTree(TreeNode** pNode1 , TreeNode** pNode2)

 13 {

 14     TreeNode* A = new TreeNode();

 15     A->m_nValue = 'A';

 16     TreeNode* B = new TreeNode();

 17     B->m_nValue = 'B';

 18     TreeNode* C = new TreeNode();

 19     C->m_nValue = 'C';

 20     TreeNode* D = new TreeNode();

 21     D->m_nValue = 'D';

 22     TreeNode* E = new TreeNode();

 23     E->m_nValue = 'E';

 24     TreeNode* F = new TreeNode();

 25     F->m_nValue = 'F';

 26     TreeNode* G = new TreeNode();

 27     G->m_nValue = 'G';

 28     TreeNode* H = new TreeNode();

 29     H->m_nValue = 'H';

 30     TreeNode* I = new TreeNode();

 31     I->m_nValue = 'I';

 32     TreeNode* J = new TreeNode();

 33     J->m_nValue = 'J';

 34 

 35 

 36     A->m_vChildren.push_back(B);

 37     A->m_vChildren.push_back(C);

 38     B->m_vChildren.push_back(D);

 39     B->m_vChildren.push_back(E);

 40     D->m_vChildren.push_back(F);

 41     D->m_vChildren.push_back(G);

 42     E->m_vChildren.push_back(H);

 43     E->m_vChildren.push_back(I);

 44     E->m_vChildren.push_back(J);

 45 

 46     *pNode1 = F;

 47     *pNode2 = G;

 48     return A;

 49 }

 50 

 51 

 52 

 53 bool GetNodePath(TreeNode* pRoot , TreeNode* pNode , list<TreeNode*>& path)

 54 {

 55     if (pRoot == pNode)

 56     {

 57         return true;

 58     }

 59     bool find = false ;

 60     path.push_back(pRoot);

 61     vector<TreeNode*>::iterator it = pRoot->m_vChildren.begin();

 62     while (!find && it != pRoot->m_vChildren.end())

 63     {

 64         find = GetNodePath(*it ,pNode , path ) ;

 65         it++ ;

 66     }

 67     if (!find)

 68     {

 69         path.pop_back();

 70     }

 71     return find;

 72 }

 73 

 74 TreeNode* GetLastCommonNode(list<TreeNode*>& path1 , list<TreeNode*>& path2 )

 75 {

 76     if (path1.empty() || path2.empty())

 77     {

 78         return NULL;

 79     }

 80     list<TreeNode*>::const_iterator it1 = path1.begin();

 81     list<TreeNode*>::const_iterator it2 = path2.begin();

 82     TreeNode* temp = NULL ;

 83     while ( it1 != path1.end() && it2 != path2.end() && (*it1 == *it2))

 84     {

 85         temp = *it1 ;

 86         it1++;

 87         it2++;

 88     }

 89 

 90     return temp;

 91 }

 92 

 93 TreeNode* GetLastCommonParent(TreeNode* pRoot, TreeNode* pNode1, TreeNode* pNode2)

 94 {

 95     if (pRoot == NULL || pNode1 == NULL || pNode2 == NULL)

 96     {

 97         return NULL;

 98     }

 99     list<TreeNode*> path1 ;

100     GetNodePath(pRoot, pNode1, path1);

101 

102     list<TreeNode*> path2 ;

103     GetNodePath(pRoot, pNode2, path2);

104     

105     return GetLastCommonNode(path1, path2);

106 }

107 

108 

109 

110 int main()

111 {

112     TreeNode *pNode1 = NULL , *pNode2 = NULL ;

113     TreeNode* pRoot = ConstructTree(&pNode1, &pNode2);

114     TreeNode* LastCommonParent = GetLastCommonParent(pRoot, pNode1, pNode2);

115 

116     cout<<LastCommonParent->m_nValue<<endl;

117     getchar();

118     return 0;

119 

120 }

 

你可能感兴趣的:(树)