不是BST,那么搜索两节点的LCA就复杂点了,由于节点是无序的。

以下是两种方法,都写进一个类里面了。

当然须要反复搜索的时候。能够使用多种方法加速搜索。

#include <iostream>
#include <vector>
using namespace std; class LCANormalTree
{
struct Node
{
int key;
Node *left, *right;
Node(int k) : key(k) , left(NULL), right(NULL) {}
}; //method 1:
bool findPath(Node *root, vector<Node *> &path, int k)
{
if (!root) return false;
path.push_back(root);
if (root->key == k) return true;
if (findPath(root->left, path, k) || findPath(root->right, path, k))
return true; path.pop_back();
return false;
} Node *findLCA(Node *root, int n1, int n2)
{
vector<Node *> pathN1, pathN2;
if (!findPath(root, pathN1, n1) || !findPath(root, pathN2, n2))
return NULL; int i = 0;
for (; i < (int) pathN1.size() && pathN1[i] == pathN2[i]; i++);
return pathN1[i-1];
} //Method 2:
Node *findLCAUtil(Node *root, int n1, int n2, bool &v1, bool &v2)
{
if (!root) return NULL;
if (root->key == n1)
{
v1 = true;
return root;
}
if (root->key == n2)
{
v2 = true;
return root;
}
Node *left = findLCAUtil(root->left, n1, n2, v1, v2);
Node *right = findLCAUtil(root->right, n1, n2, v1, v2); if (left && right) return root;
return left? left:right;
} bool find(Node *root, int k)
{
if (!root) return false;
if (root->key == k || find(root->left, k) || find(root->right, k))
return true;
return false;
} Node *findLCA_2(Node *root, int n1, int n2)
{
bool v1 = false, v2 = false;
Node *lca = findLCAUtil(root, n1, n2, v1, v2);
//当两者在不同一边的时候v1&&v1,当一个为另外一个的单亲节点的时候,那么就出现后面两种情况了。
if (v1 && v2 || v1 && find(lca, n2) || v2 && find(lca, n1))
return lca;
return NULL;
} Node *root;
public:
LCANormalTree()
{
cout<<"Algorithm 1 run\n";
run_1();
cout<<"\nAlgorithm 2 run\n";
run_2();
} void run_1()
{
root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
Node *t = findLCA(root, 4, 5);
cout << "LCA(4, 5) = " << (t? t->key : -1);
t = findLCA(root, 4, 6);
cout << "\nLCA(4, 6) = " << (t? t->key : -1);
t = findLCA(root, 3, 4);
cout << "\nLCA(3, 4) = " << (t? t->key : -1);
t = findLCA(root, 2, 4);
cout << "\nLCA(2, 4) = " << (t? t->key : -1);
cout << endl; deleteTree(root);
} void run_2()
{
root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
Node *lca = findLCA(root, 4, 5);
if (lca != NULL)
cout << "LCA(4, 5) = " << lca->key;
else
cout << "Keys are not present "; lca = findLCA(root, 4, 10);
if (lca != NULL)
cout << "\nLCA(4, 10) = " << lca->key;
else
cout << "\nKeys are not present "; cout<<endl;
deleteTree(root);
} ~LCANormalTree()
{
if (root) deleteTree(root);
} void deleteTree(Node *&r)
{//注意不能是*r。要*&r,由于须要改动r为NULL,防止多次释放
if (r)
{
deleteTree(r->left);
deleteTree(r->right);
delete r; r = NULL;
}
}
};

Geeks 一般二叉树的LCA的更多相关文章

  1. 二叉树的LCA(最近公共祖先)算法

    1.如果是二叉搜索树 2.如果是普通树

  2. 最近公共祖先问题 LCA

    2018-03-10 18:04:55 在图论和计算机科学中,最近公共祖先,LCA(Lowest Common Ancestor)是指在一个树或者有向无环图中同时拥有v和w作为后代的最深的节点. 计算 ...

  3. Leetcode_236. 二叉树的最近公共祖先

    求二叉树的LCA code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le ...

  4. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  5. 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)

    寻找最近公共祖先,示例如下: 1 /           \ 2           3 /    \        /    \ 4    5      6    7 /    \          ...

  6. PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. 面试题6:二叉树最近公共节点(LCA)《leetcode236》

    Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...

  8. PAT 1151 LCA in a Binary Tree[难][二叉树]

    1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  9. Geeks LCA最低公共单亲节点

    给出一颗二叉树.找到两个值的最小公共节点. 假设两个值都会在树中出现. 假设可能不会出现的话,也非常easy.就查找一遍看两个值是否在树中就能够了.假设不在就直接返回NULL. 基本思想:就是在二叉树 ...

随机推荐

  1. tomcat数据源配置DBCP

    原文件: https://www.cnblogs.com/sicd/p/4053780.html DBCP object created 日期 by the following code was ne ...

  2. java IO流之——File类知识总结和面试

    File类描述的是一个文件或文件夹.(文件夹也可以称为目录).该类的出现是对文件系统的中的文件以及文件夹进行对象的封装.可以通过对象的思想来操作文件以及文件夹.可以用面向对象的处理问题,通过该对象的方 ...

  3. volatile 学习笔记

    全面理解Java内存模型(JMM)及volatile关键字 正确使用 Volatile 变量 Java内存模型 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步.通信是指线程之间 ...

  4. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  5. 专业分析docker的分层存储技术

    话不在多,指明要点! 联合挂载是用于将多个镜像层的文件系统挂载到一个挂载点来实现一个统一文件系统视图的途径, 是下层存储驱动(aufs.overlay等) 实现分层合并的方式. 所以严格来说,联合挂载 ...

  6. Asp.net mvc 实时生成缩率图到硬盘

    之前对于缩率图的处理是在图片上传到服务器之后,同步生成两张不同尺寸的缩率供前端调用,刚开始还能满足需求,慢慢的随着前端展示的多样化,缩率图已不能前端展示的需求,所以考虑做一个实时生成图片缩率图服务. ...

  7. day5模块学习 -- time、datetime时间模块

    1.定义 模块:用来从逻辑上组织python(变量,函数,类,逻辑:实现一个功能)代码,本质就是.py结尾的python文件(文件名:test.py,对应的模块名test) 包:用来从逻辑上组织模块的 ...

  8. linux 101 hacks 7crontab

    技巧 74: crontab 书上的这一段我删了,重写一下,用的是ubuntu 16.04系统 参考 每天一个 linux 命令(50):crontab 命令 crond 服务 安装 crontab: ...

  9. C语言:九九乘法表打印

    题目: 要求:用“,”分隔算式,用“:”做一行的结尾. 另外1*1=1:这个算式是程序的第一行,前面没有空行. 文字版如下: 输入格式: 无 输出格式: 1*1=1; 2*1=2,2*2=4; 3*1 ...

  10. 【LOJ】#2587. 「APIO2018」铁人两项

    题解 学习了圆方树!(其实是复习了Tarjan求点双) 我又双叒叕忘记了tarjan点双一个最重要,最重要的事情! 就是--假如low[v] >= dfn[u],我们就找到了一个点双,开始建立方 ...