剑指Offer—树中两个节点的最低公共祖先

/////////////////主文件

#include <iostream>

#include <vector>

#include <list>

#include “Tree.h”

using namespace std;

bool FindPath(TreeNode* pRoot, TreeNode* pNode, list<TreeNode*>& tree_list)

{

if (pRoot == pNode)

return true;

tree_list.push_back(pRoot);

bool found = false;

vector<TreeNode*>::iterator iter = pRoot->m_children.begin();

while (!found&&iter<pRoot->m_children.end())

{

found=FindPath(*iter, pNode, tree_list);
//递归进行入队列

iter++;

}

if (!found)

{

tree_list.pop_back();
//没有找到节点,则出队列,进行回溯遍历

}

return found;

}

TreeNode* GetLastCommonNode(list<TreeNode*> tree_listx, list<TreeNode*> tree_listy)

{

list<TreeNode*>::iterator iter1 = tree_listx.begin();

list<TreeNode*>::iterator iter2 = tree_listy.begin();

TreeNode* pResult = NULL;

while (iter1!=tree_listx.end()&&iter2!=tree_listy.end())

{

if (*iter1 == *iter2)

pResult = *iter1;

iter1++;

iter2++;

}

return pResult;

}

TreeNode* FindCommonGrand(TreeNode* pRoot, TreeNode* pNodex, TreeNode* pNodey)

{

if (pRoot == NULL || pNodex == NULL || pNodey == NULL)

return NULL;

list<TreeNode*> tree_listx;

FindPath(pRoot, pNodex,tree_listx);

list<TreeNode*> tree_listy;

FindPath(pRoot, pNodey, tree_listy);

return GetLastCommonNode(tree_listx, tree_listy);

}

int main()

{

TreeNode* pNode1=CreatTreeNode(1);

TreeNode* pNode2 = CreatTreeNode(2);

TreeNode* pNode3 = CreatTreeNode(3);

TreeNode* pNode4 = CreatTreeNode(4);

TreeNode* pNode5 = CreatTreeNode(5);

TreeNode* pNode6 = CreatTreeNode(6);

TreeNode* pNode7 = CreatTreeNode(7);

TreeNode *pNode8 = CreatTreeNode(8);

TreeNode *pNode9 = CreatTreeNode(9);

TreeNode *pNode10 = CreatTreeNode(10);

ConnectTreeNode(pNode1, pNode2);

ConnectTreeNode(pNode1, pNode3);

ConnectTreeNode(pNode2, pNode4);

ConnectTreeNode(pNode2, pNode5);

ConnectTreeNode(pNode4, pNode6);

ConnectTreeNode(pNode4, pNode7);

ConnectTreeNode(pNode5, pNode8);

ConnectTreeNode(pNode5, pNode9);

ConnectTreeNode(pNode5, pNode10);

TreeNode* pRoot = pNode1;

PrintTree(pRoot);

TreeNode* pNodex = pNode7;

TreeNode* pNodey = pNode9;

TreeNode* pRes=FindCommonGrand(pRoot, pNodex, pNodey);

cout << “最近公共节点:” << pRes->val << endl;

system(“pause”);

return 0;

}

/////////////////文件tree.h

#ifndef TREE_H
#define TREE_H

#include <vector>
#include <iostream>
using namespace std;
struct TreeNode
{
int val;
vector<TreeNode*> m_children;
};

TreeNode* CreatTreeNode(int);
void ConnectTreeNode(TreeNode* pParent, TreeNode* pChild);
void PrintTreeNode(TreeNode* pNode);
void PrintTree(TreeNode* pRoot);
#endif

/////////////////文件tree.cc

#include “tree.h”

TreeNode* CreatTreeNode(int val)
{
TreeNode* pNode = new TreeNode();
pNode->val = val;
return pNode;
}
void ConnectTreeNode(TreeNode* pParent, TreeNode* pChild)
{
if (pParent!=NULL)
{
pParent->m_children.push_back(pChild);
}
}
void PrintTreeNode(TreeNode* pNode)
{
if (pNode != NULL)
{
cout << “树是:” <<pNode->val<<endl;
if (pNode->m_children.empty())
{
cout << “树没有子节点!”;
}
else
{
cout << “树的子节点是:”;
vector<TreeNode*>::iterator iter = pNode->m_children.begin();
while (iter != pNode->m_children.end())
{
if (*iter != NULL)
cout << (*iter)->val << “,”;
iter++;
}
}
cout << endl;
}
else
cout << “树是空的” << endl;
}
void PrintTree(TreeNode* pRoot)
{
PrintTreeNode(pRoot);
if (pRoot!=NULL)
{
vector<TreeNode*>::iterator iter = pRoot->m_children.begin();
while (iter!=pRoot->m_children.end())
{
PrintTree(*iter);
iter++;
}
}
}

点赞