不是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. 洛谷P3367并查集

    传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...

  2. DevExpress 去除皮肤的方法

    我从不用皮肤,方法如下:

  3. nginx重写链接

    location ~ /send.redir {             if ( $query_string ~ “url=(?<path>.+)” ) {rewrite ^.* htt ...

  4. 数据库中INFORMATION_SCHEMA的说明及使用

    第一个查询看看库里有多少个表,表名等select * from INFORMATION_SCHEMA.TABLES information_schema这张数据表保存了MySQL服务器所有数据库的信息 ...

  5. 使用django发送邮件时的连接超时问题解决

    一.报错 研究报错半天,没看出代码有什么毛病,就是发送邮件时连接超时,发送邮件的连接用户名密码都没有错误,于是就网上各种查... 终于皇天不负有心人,找到答案了.. 在服务器上输入telnet smt ...

  6. MySQL学习笔记:时间差

    1.MySQL计算同一张表中同一列的时间差,同一个id,有多个时间,求出每个id最早时间和最晚时间之间的差值. 原始表如下: 查询语句: SELECT id, MAX(TIME), MIN(TIME) ...

  7. Chrome+XX_Net的上网渠道

    本页面将详细演示如何在一台全新的Windows7电脑上架设起Chrome+XX_Net的上网渠道. 本文包含以下部分: 下载和安装Chrome浏览器 获取和运行XX-Net 设置代理 手动导入证书(备 ...

  8. SourceTree 3.0.17如何跳过注册进行安装? — git图形化工具(一)

    SourceTree 3.0.17个人版本的尝试跳过注册方式好几次都没成功,于是下载了企业版本https://www.sourcetreeapp.com/enterprise. 安装过程: 1.首次点 ...

  9. sql server2014 企业版 百度云下载

    sql server2014 企业版 百度云下载 链接: https://pan.baidu.com/s/1j7a6RWwpvSzG-sF7Dnexfw 提取码: 关注公众号[GitHubCN]回复获 ...

  10. CCF CSP 201512-2 消除类游戏

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201512-2 消除类游戏 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行 ...