不是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. java基础33 Set集合下的HashSet集合和TreeSet集合

    单例集合体系: ---------| collection  单例集合的根接口--------------| List  如果实现了list接口的集合类,具备的特点:有序,可重复       注:集合 ...

  2. thinkphp5 IIS7.5 隐藏index.php的方法

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  3. day7 socket网络编程基础

    Socket Socket是什么? 下面来看一下网络的传输过程: 上面图片显示了网络传输的基本过程,传输是通过底层实现的,有很多底层,我们写传输过程的时候,要知道所有的过程那就太复杂了,socket为 ...

  4. 【LOJ】#2052. 「HNOI2016」矿区

    题解 之前尝试HNOI2016的时候弃坑的一道,然后给补回来 (为啥我一些计算几何就写得好长,不过我写啥都长orz) 我们尝试给这个平面图分域,好把这个平面图转成对偶图 怎么分呢,我今天也是第一次会 ...

  5. 2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  6. phpstorm 输入法中文不同步 phpstorm 输入法不跟随光标解决办法

    win7系统新安装的phpstorm2017.2版本,试了很多输入法,要么是不显示候选次,要么是输入法候选词总是在屏幕右下角,没有跟随光标移动.百度很久,重要找到解决方案. 就是替换phpstorm安 ...

  7. Spring Boot 教程demo

    https://github.com/ityouknow/spring-boot-examples

  8. java把html标签字符转换成普通字符(反转换成html标签)

    package net.jasonjiang.web; import org.junit.Test; import org.springframework.web.util.HtmlUtils; /* ...

  9. Python并发编程-IO模型-非阻塞IO实现SocketServer

    Server.py import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.setblocking(False) #把soc ...

  10. mysql高性能索引

    独立索引: 独立索引是指索引列不能是表达式的一部分,也不能是函数的参数 例1: SELECT actor_id FROM actor WHERE actor_id+1=5 --这种写法,就算在acto ...