[Leetcode 235/236]LCA二叉树最近公共祖先Lowest Common Ancestor of a Binary Tree
题目
给定二叉树和两个点,求两点的LCA最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [1,2], p = 1, q = 2
Output: 1
思路
是一道套路题,A B俩点的位置只有三种情况
- A B 都在root左边(这时LCA必也在左边)
- A B 都在root右边(这时LCA必也在右边)
- A B 分别在root的左右边(这时LCA必为root)
代码
写起来很简单,但没想通为什么回溯之后得到的就是LCA,怎么证明
想通了,必定是LCA。比如俩点都在left,最后fun(root.left,p,q)就是LCA
因为fun(root.left,p,q)再次调用fun
fun(root.left.left,p,q)和fun(root.left.right,p,q)
只有当left和right都不为null时,才返回root(此时的root可能是root.left.left/root.left.right.left等等)
所以,虽然代码中是root==p或root==q,看似只找一个点,其实是俩点都找到back时才返回结果
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==p||root==q||root==null)
return root;
TreeNode leftRoot=lowestCommonAncestor(root.left,p,q);
TreeNode rightRoot=lowestCommonAncestor(root.right,p,q);
if (leftRoot==null)
return rightRoot;//root的left没有pq,即pq都在右边,LCA也在右边
else if(rightRoot==null)
return leftRoot;//root的right没有pq,即pq都在左边,LCA也在左
else
return root;//俩节点分别在root的左右俩边,那LCA就是rott本身
}
}
[Leetcode 235/236]LCA二叉树最近公共祖先Lowest Common Ancestor of a Binary Tree的更多相关文章
- [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 ...
- [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 ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [LeetCode] 236. 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 ...
- [LeetCode] 236. 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 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- yaml 文件的读取写
yaml 是一种数据格式, 它可以和json数据相互转化 . 自动化测试中一般用于做配置文件或是测试用例. 数据的组成, 两种格式: 1. 字典 2. 列表 Eg. config.yaml serve ...
- pgsql的round函数
不知道是我菜还是咋地,感觉pg里面用round不是很爽啊,明明在其他库能运行的,字段类型卡得太死了吧 照说float8类型还是数值,怎么就报错呢,如下 错误: 函数 round(double pre ...
- k8s_使用k8s部署wordpress博客系统(一)
系统部署流程 使⽤kubernetes部署wordpress+MySQL, 并利⽤NFS去保存我们容器的源代码以及DB数据.搭建好nfs后任意node上的Pod访问db或者业务代码都会有相同的效果,数 ...
- Kafka Reblance & max.poll.interval.ms 重复消费问题
1. 什么是kafka Reblance 消费组是MQ中一个非常重要的概念,一个消费组监听一个Topic时,Kafka服务端会给消费组中的每一个实例,进行队列分配,每一个实例指定消费一个或多个队列(分 ...
- Docker的资源限制
CPU CGROUP MEM CGROUP Storage 默认的overlay fs不支持配额,需要底层文件系统如xfs,ext4的支持. 在docker中启用示例如下,限制单个容器最大使用空间为2 ...
- taobao.tbk.sc.newuser.order.get( 淘宝客-服务商-新用户订单明细查询 )
淘宝客订单表结构设计(mysql) CREATE TABLE `tbk_order` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `member_id` bi ...
- 安卓开发学习10-1:数据存储:Shared Preferences存储
解析 什么是Shared Perferences 应用场景 配置信息 主题信息 游戏的积分信息等 存储路径 在本应用中的data-data-应用包-自定义名称xml文件下保存写入的数据信息 使用 获取 ...
- Expected space(s) after "default" keyword-spacing
添加空格
- 2022-3-18内部群每日三题-清辉PMP
1.在评估项目活动现状的会议中,团队发现存在一些影响可交付成果的风险.项目经理应该怎么做? A.跟踪已发现的风险,识别和分析新风险,并评估整个项目的风险过程有效性 B.记录风险管理信息用于经验教训 C ...
- element ui 分页
修改选中页码的样式: .el-pager .active{ color:red !important;//选中 } .el-pager .number:hover{ color:red !import ...