树的公共祖先问题

#include<iostream>
#include<vector>
#include<list>
using namespace std;
struct TNode
{
    int key;
    vector<TNode *> m_vchildren;
};
int GetNodePath(TNode *pRoot, TNode *pNode, list<TNode *>&path)
{
    if (pRoot == pNode)
        return true;
    path.push_back(pRoot);
    int found = false;
    vector<TNode *>::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;
}
TNode * GetLastCommonNode(const list<TNode *> &path1, const list <TNode *> &path2)
{
    list<TNode*>::const_iterator iterator1 = path1.begin();
    list<TNode*>::const_iterator iterator2 = path2.begin();
    TNode * pLast = NULL;
    while (iterator1 != path1.end() && iterator2 != path2.end())
    {
        if (*iterator1 != *iterator2)
        {
            pLast = *(--iterator1);
            break;
        }
        iterator1++;
        iterator2++;
    }
    if (iterator1 == path1.end())
        pLast = *(--iterator2);
    if (iterator2 == path2.end())
        pLast = *(--iterator2);
    return pLast;
}
TNode *GetLastCommonParent(TNode *pRoot, TNode *pNode1, TNode *pNode2)
{
    if (pRoot == NULL || pNode1 == NULL || pNode2 == NULL)
        return NULL;
    list<TNode *> path1;
    list<TNode *>::iterator iter;
    GetNodePath(pRoot, pNode1, path1);
    list<TNode *> path2;
    GetNodePath(pRoot, pNode2, path2);
    return GetLastCommonNode(path1, path2);
}
int main()
{
    TNode a, b, c, d, e, f, g, h, i, j, k;
    i.key = 9;
    j.key = 10;
    k.key = 11;
    h.key = 8;
    g.key = 7;
    f.key = 6;
    f.m_vchildren.push_back(&i);
    f.m_vchildren.push_back(&j);
    f.m_vchildren.push_back(&k);
    e.key = 5;
    d.key = 4;
    c.key = 3;
    c.m_vchildren.push_back(&g);
    c.m_vchildren.push_back(&h);
    b.key = 2;
    b.m_vchildren.push_back(&d);
    b.m_vchildren.push_back(&e);
    b.m_vchildren.push_back(&f);
    a.key = 1;
    a.m_vchildren.push_back(&b);
    a.m_vchildren.push_back(&c);
    cout << GetLastCommonParent(&a, &d, &i)->key << endl;
    return 0;
}
#include<iostream>
using namespace std;
struct TreeNode
{
    int key;
    TreeNode * lchild;
    TreeNode * rchild;
};
TreeNode* getLCA(TreeNode* root, TreeNode *X, TreeNode *Y)
{
    if (root == NULL) return NULL;
    if (X == root || Y == root) return root;
    TreeNode *left = getLCA(root->lchild, X, Y);
    TreeNode *right = getLCA(root->rchild, X, Y);
    if (left == NULL) return right;
    else if (right == NULL) return left;
    else return root;
}
int main()
{
    TreeNode g = { 7, NULL, NULL }, f = { 6, NULL, NULL }, e = { 5, &f, &g }, d = { 4, NULL, NULL }, c = { 3, NULL, NULL }, b = { 2, &d, &e }, a = { 1, &b, &c };
    TreeNode *p;
    p = getLCA(&a, &g, &f);
    cout << p->key << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值