基于深度的LCA算法:  对于两个结点u、v,它们的深度分别为depth(u)、depth(v),对于其公共祖先w,深度为depth(w),u需要向上回溯depth(u)-depth(w)步,v需要depth(v)-depth(w)步。 因此,只需要u,v中深度较大的向上移,直到 depth(u)=depth(v) && 此时指向一个结点。

因此,求出LCA的时间复杂度为O(depth(u)+depth(v));   求深度,只需要进行一次遍历,O(n)。

如果是完全二叉树的话,相当于深度(下标)已经建立好了,直接根据LCA进行计算。

 vector<int> G[MAXV];  // 树
int root; int parent[MAXV]; // 父节点; (并查集)
int depth[MAXV]; // 节点深度 // v:当前结点 p:父节点 d:深度
void DFS(int v,int p,int d){
parent[v]=p;
depth[v]=d;
for(auto& p:G[v]){
dfs(p,v,d+);
}
} void init(){
dfs(root,-,);
} int LCA(int u,int v){
while(depth[u]>depth[v]){
u=parent[u];
}
while(depth[u]<depth[v]){
v=parent[v];
}
while(u!=v){
u=parent[u];
v=parent[v];
}
return u;
}

如果需要查找多对LCA,可以考虑二分查找的性质,来找到最小步数。

最近公共祖先 Lowest Common Ancestors的更多相关文章

  1. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  2. [Swift]LeetCode236. 二叉树的最近公共祖先 | Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  3. 最近公共祖先 · Lowest Common Ancestor

    [抄题]: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. “Th ...

  4. 最近公共祖先(least common ancestors algorithm)

    lca问题是最近公共祖先问题,一般是针对树结构的.现在有两种方法来解决这样的问题 1. On-line algorithm 用比较长的时间做预处理.然后对每次询问进行回答. 思路:对于一棵树中的两个节 ...

  5. 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换

    [简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...

  6. 最近公共祖先 LCA (Lowest Common Ancestors)-树上倍增

    树上倍增是求解关于LCA问题的两个在线算法中的一个,在线算法即不需要开始全部读入查询,你给他什么查询,他都能返回它们的LCA. 树上倍增用到一个关键的数组F[i][j],这个表示第i个结点的向上2^j ...

  7. 最近公共祖先 LCA 倍增算法

          树上倍增求LCA LCA指的是最近公共祖先(Least Common Ancestors),如下图所示: 4和5的LCA就是2 那怎么求呢?最粗暴的方法就是先dfs一次,处理出每个点的深度 ...

  8. 编程算法 - 二叉树的最低公共祖先 代码(C)

    二叉树的最低公共祖先 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉树的最低公共祖先(lowest common ancestor), 首先先序遍 ...

  9. LeetCode 235. 二叉搜索树的最近公共祖先 32

    235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树,找到该树中两个指定节点的最近公 ...

随机推荐

  1. NServiceBus 入门到精通(一)

    什么是NServiceBus?NServiceBus 是一个用于构建企业级 .NET系统的开源通讯框架.它在消息发布/订阅支持.工作流集成和高度可扩展性等方面表现优异,因此是很多分布式系统基础平台的理 ...

  2. 微软帮助类SqlHelper

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  3. Android实战项目——家庭记账本(六)

    今天完成的主要任务如下: 1.设置页功能的布局 2.云服务器的部署 3.成功将一个本地Javaweb项目部署到阿里云 没什么特别说明的,直接上图: 首先是侧边栏功能的布局:               ...

  4. centos 7.6 docker 安装 nextcloud -使用sqlite数据库

    docker search nextcloud docker pull docker.io/nextcloud docker images mkdir /home/nextcloud chmod -R ...

  5. jdk1.7、jdk1.8兼容共存环境配置方案

    前提:环境变量配置 JAVA_HOME=%JAVA_HOME8% JAVA_HOME8=E:\jdk1.8.0_40 JAVA_HOME7=E:\jdk1.7.0_51 path 一.jdk是绿色免安 ...

  6. Anaconda 包管理与环境管理

    包管理命令 conda命令 安装包 conda install 包名称 卸载包 conda remove 包名称 更新包 conda update 包名称 模糊查询 conda search 包名称 ...

  7. 浅谈python的第三方库——numpy(二)

    前一期博文中,初步探索了numpy中矩阵的几种运算操作,本文将展示numpy矩阵的元素抽取与合并操作. 1 元素抽取 在我们使用矩阵的时候,有时需要提取出矩阵的某些位置上的元素单独研究,这时就需要熟悉 ...

  8. [TJOI2008] 小偷

    TJOI2008小偷 题目背景 一位著名的小偷进入了一个充满宝石的储藏室,这个储藏室是由一连串房间构成的,房间的标号从0开始,想进入第i个房间就必须从第i-1个房间进入,如图: 题目描述 上图为三个房 ...

  9. Wannafly Winter Camp 2020 Day 7E 上升下降子序列 - 数学

    神奇公式 #include <bits/stdc++.h> using namespace std; #define int long long int n,mod,c[205][205] ...

  10. HashMap (JDK1.8) 分析

    一.HashMap(JDK1.8) 1.基本知识.数据结构 (1)时间复杂度:用来衡量算法的运行时间. 参考:https://blog.csdn.net/qq_41523096/article/det ...