Geeks 一般二叉树的LCA
不是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的更多相关文章
- 二叉树的LCA(最近公共祖先)算法
1.如果是二叉搜索树 2.如果是普通树
- 最近公共祖先问题 LCA
2018-03-10 18:04:55 在图论和计算机科学中,最近公共祖先,LCA(Lowest Common Ancestor)是指在一个树或者有向无环图中同时拥有v和w作为后代的最深的节点. 计算 ...
- Leetcode_236. 二叉树的最近公共祖先
求二叉树的LCA code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
寻找最近公共祖先,示例如下: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ ...
- 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 ...
- 面试题6:二叉树最近公共节点(LCA)《leetcode236》
Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...
- 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 ...
- Geeks LCA最低公共单亲节点
给出一颗二叉树.找到两个值的最小公共节点. 假设两个值都会在树中出现. 假设可能不会出现的话,也非常easy.就查找一遍看两个值是否在树中就能够了.假设不在就直接返回NULL. 基本思想:就是在二叉树 ...
随机推荐
- github后端开发面试题大集合(一)
作者:小海胆链接:https://www.nowcoder.com/discuss/3614?type=0&order=0&pos=5&page=0?from=wb来源:牛客网 ...
- DEDECMS去除后门隐患和漏洞以及冗余代码的方法
链接:http://jingyan.baidu.com/article/4d58d541195bdb9dd4e9c029.html 工具/原料 织梦网站管理系统 sublime编辑器 方法/步骤 第一 ...
- python版本共存
要玩多版本最好使用虚拟环境,避免根python切换及包误安装的麻烦 1.直接安装实现 1.1 windows下 到官网(https://www.python.org/downloads/)下载,如py ...
- SonarQube代码质量管理工具的安装(Linux)
一.安装配置sonar 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具, ...
- [C++]返回最值元素
1 priority_queue C++中优先队列是一种特殊的队列,能够返回队列中优先级最大或者最小的元素,其内部是由堆实现的,个人认为这种方式使用更加直观. 1.1 返回vector中的最值元素 # ...
- iOS中URL的解码和转义问题
在iOS开发中,使用NSURLConnection去请求google places api时,如果请求的url中包含中文,则返回的结果为空,URL不能被google识别.NSString *_urlS ...
- information that should help you find out what is causing the crash.
091130 11:16:11 - mysqld got exception 0xc0000005 ; This could be because you hit a bug. It is also ...
- python开发学习-day13(js、jQuery)
s12-20160409-day13 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- react篇章-React State(状态)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- CSUOJ 1895 Apache is late again
Description Apache is a student of CSU. There is a math class every Sunday morning, but he is a very ...