基于深度的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. Android中实现长按照片弹出右键菜单

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为Lin ...

  2. 范式通俗理解:1NF、2NF、3NF和BNCF

    https://blog.csdn.net/wyh7280/article/details/83350722 范式通俗理解:1NF.2NF.3NF和BNCF原创hongiii 最后发布于2018-10 ...

  3. 【React Native】集成声网Agora语音通讯

    前言: 公司的产品是一款基于社交的内容聊天软件,需要集成语音通讯功能,在写iOS原生项目时,用到的就是Agora SDK,现在写React Native也直接采用了Agora的库. 集成iOS.And ...

  4. DockerFile优化总结

    1- dockerFile为什么要优化 ___ 随着我们对docker镜像的持续使用,在此过程中如果不加以注意并且优化,镜像的体积会越来越多.很多时候我们在使用docker部署应用时,会发现镜像的体积 ...

  5. ie8兼容rgba写法

    ie使用filter解决半透明兼容性问题 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#19ffffff,endCo ...

  6. Selenium实战(四)——unittest单元测试3(测试用例的执行顺序)

    一.测试用例的执行顺序 层级:多个测试目录 > 多个测试文件 > 多个测试类 > 多个测试方法(测试用例).在这里以一个测试执行顺序的脚本为例test_order.py import ...

  7. Enable VT-x in your BIOS security settings, ensure that HAXM is installed properly. Try disabling 3rd party security software if the problem still occurs

    win7系统安装了了AndroidStudio3.5后用模拟器报错:Enable VT-x in your BIOS security settings, ensure that HAXM is in ...

  8. kaggle之猫狗数据集下载

    链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw 提取码:2xq4 百度网盘实在是恶心,找的别人的网盘下载不仅速度慢,还老挂掉,自己去kaggle下 ...

  9. Chrome 插件 postman 可以在线post

    地址:https://chrome.google.com/webstore/detail/fhbjgbiflinjbdggehcddcbncdddomop

  10. SpringCloud(一)

    什么是SpringCloud? Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...