首页 > 代码库 > 求一棵普通树的两个结点的最低公共祖先
求一棵普通树的两个结点的最低公共祖先
一棵普通树,树中的结点没有指向父节点的指针,求一棵普通树的两个结点的最低公共祖先。
代码如下,我太懒没有加注释,大家自己看吧!
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 = http://www.mamicode.com/‘A‘; 16 TreeNode* B = new TreeNode(); 17 B->m_nValue = http://www.mamicode.com/‘B‘; 18 TreeNode* C = new TreeNode(); 19 C->m_nValue = http://www.mamicode.com/‘C‘; 20 TreeNode* D = new TreeNode(); 21 D->m_nValue = http://www.mamicode.com/‘D‘; 22 TreeNode* E = new TreeNode(); 23 E->m_nValue = http://www.mamicode.com/‘E‘; 24 TreeNode* F = new TreeNode(); 25 F->m_nValue = http://www.mamicode.com/‘F‘; 26 TreeNode* G = new TreeNode(); 27 G->m_nValue = http://www.mamicode.com/‘G‘; 28 TreeNode* H = new TreeNode(); 29 H->m_nValue = http://www.mamicode.com/‘H‘; 30 TreeNode* I = new TreeNode(); 31 I->m_nValue = http://www.mamicode.com/‘I‘; 32 TreeNode* J = new TreeNode(); 33 J->m_nValue = http://www.mamicode.com/‘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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。