二叉树的最低公共祖先 代码(C)

本文地址: http://blog.csdn.net/caroline_wendy

二叉树的最低公共祖先(lowest common ancestor), 首先先序遍历找到两个结点的路径, 然后依据链表路径找到最低的公共祖先.

代码:

/*
* main.cpp
*
* Created on: 2014.6.12
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <list>
#include <queue> using namespace std; struct BinaryTreeNode {
BinaryTreeNode(int _value) {
value = _value;
left = NULL;
right = NULL;
} int value;
BinaryTreeNode* left;
BinaryTreeNode* right;
}; void printTree (BinaryTreeNode* tree)
{
BinaryTreeNode* node = tree;
std::queue<BinaryTreeNode*> temp1;
std::queue<BinaryTreeNode*> temp2; temp1.push(node); while (!temp1.empty())
{
node = temp1.front();
if (node->left != NULL) {
temp2.push(node->left);
} if (node->right != NULL) {
temp2.push(node->right);
} temp1.pop(); std::cout << node->value << " "; if (temp1.empty())
{
std::cout << std::endl;
temp1 = temp2;
std::queue<BinaryTreeNode*> empty;
std::swap(temp2, empty);
}
}
} BinaryTreeNode* buildTree (void)
{
BinaryTreeNode* root = new BinaryTreeNode(1);
BinaryTreeNode* node2 = new BinaryTreeNode(2);
BinaryTreeNode* node3 = new BinaryTreeNode(3);
BinaryTreeNode* node4 = new BinaryTreeNode(4);
BinaryTreeNode* node5 = new BinaryTreeNode(5);
BinaryTreeNode* node6 = new BinaryTreeNode(6);
BinaryTreeNode* node7 = new BinaryTreeNode(7);
BinaryTreeNode* node8 = new BinaryTreeNode(8);
BinaryTreeNode* node9 = new BinaryTreeNode(9);
BinaryTreeNode* node10 = new BinaryTreeNode(10); root->left = node2;
root->right = node3; node2->left = node4;
node2->right = node5; node4->left = node6;
node4->right = node7; node5->left = node8;
node5->right = node9; node9->left = node10; return root;
} bool GetNodePath(BinaryTreeNode* root, int v, vector<BinaryTreeNode*>& path) {
if (root->value == v)
return true;
path.push_back(root);
bool found = false;
if (root->left != NULL && !found)
found = GetNodePath(root->left, v, path);
if (root->right != NULL && !found)
found = GetNodePath(root->right, v, path);
if (!found)
path.pop_back();
return found;
} BinaryTreeNode* GetLastCommonNode (
const vector<BinaryTreeNode*>& path1, const vector<BinaryTreeNode*>& path2)
{
vector<BinaryTreeNode*>::const_iterator it1 = path1.begin();
vector<BinaryTreeNode*>::const_iterator it2 = path2.begin();
BinaryTreeNode* pLast = NULL;
while (it1 != path1.end() && it2 != path2.end()) {
if ((*it1)->value == (*it2)->value)
pLast = *it1;
it1++;
it2++;
}
return pLast;
} BinaryTreeNode* GetLastCommonParent(BinaryTreeNode* root, int v1, int v2)
{
if (root == NULL)
return NULL;
vector<BinaryTreeNode*> path1;
GetNodePath(root, v1, path1);
vector<BinaryTreeNode*> path2;
GetNodePath(root, v2, path2); return GetLastCommonNode(path1, path2);
} int main (void)
{
BinaryTreeNode* root = buildTree();
int v1 = 6;
int v2 = 10;
BinaryTreeNode* common = GetLastCommonParent(root, v1, v2);
cout << "common node : " << common->value << endl;
return 0;
}

输出:

common node : 2

最新文章

  1. bzoj1834
  2. HDU 4738 Caocao&#39;s Bridges(Tarjan求桥+重边判断)
  3. c# 多线程与异步调用
  4. metalink下载补丁包
  5. 公众号的秘密,知道一个biz就够了
  6. distributor之Interrupt Set/Clear-Active Registers, GICD_IS/CACTIVERn
  7. uva 620 Cellular Structure
  8. AFNetworking源码阅读
  9. Centos7下配置Python3和Python2共存,以及对应版本Ipython安装配置
  10. vue-router实现登录和跳转到指定页面,vue-router 传参
  11. python模拟登陆Github示例
  12. CAN总线学习记录之四:位定时与同步
  13. 实例:vue中点击空白区域关闭某个div图层
  14. Cannot forward to error page for request ......
  15. 分享一个爬取HUST(哈理工)学生成绩的Python程序(OCR自动识别验证码)
  16. ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)
  17. 网页图表Highcharts实践教程之认识Highcharts
  18. 03关于C的数组指针
  19. Scrum 敏捷开发
  20. HDU 1003 MAXSUM(最大子序列和)

热门文章

  1. linux之vim命令
  2. HTML基础一
  3. Chromatix
  4. Android服务之bindService源代码分析
  5. HDOJ 3359 Kind of a Blur
  6. 如何让mysql的自动递增的字段重新从1开始呢?(
  7. UVa 10192 - Vacation &amp;amp; UVa 10066 The Twin Towers ( LCS 最长公共子串)
  8. 8.使用JPA保存数据【从零开始学Spring Boot】
  9. cmake 如何生成一个win32工程
  10. Linux学习笔记 (三)Vi文本编辑器