[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 ...
随机推荐
- 第15周作业--JDBC连接数据库
编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功.当登录成功后,将t_user表(id.name.sex. ...
- react的生命周期和使用
完整的生命周期 我们都知道生命周期分为三个大阶段: 挂载 更新 卸载 挂载的时候我们我们有 constructor . getDerivedStateFromProps .render . compo ...
- java技术系列(二) 反射
能够分析类能力的程序称为反射. 反射能力: 在运行中分析类的能力. 在运行中查看对象,例如:编写一个toString方法供所有类使用. 实现通用的数组操作代码. 利用Method对象.
- java技术系列(三) 多线程之并行处理和同步
java同步器: 1,Semaphone: 信号量 2,CountDownLatch:倒计数门栓 3,CyclicBarrier 障栅 : 4,Exchanger :
- (四十二)Unittest单元测试框架之关于unittest还需要知道的-跳过测试和预期失败
随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...
- [BalticOI 2017] Cat in a tree
[BalticOI 2017] Cat in a tree 神仙美少女 Tweetuzki 学姐用了长剖+线段树,私以为长剖可以做到线性. 简要题意 给定 \(n\) 个点的树,点集 \(S\) 合法 ...
- 765. 情侣牵手 (Hard)
问题描述 765. 情侣牵手 (Hard) n 对情侣坐在连续排列的 2n 个座位上,想要牵到对方的手. 人和座位由一个整数数组 row 表示,其中 row[i] 是坐在第 i 个座位上的人的 ID. ...
- 几个一看就会的实用JavaScript优雅小技巧
️ 前言 这次我就给大家分享一些一看就会的实用JavaScript优雅小技巧. 「难度:」 「推荐阅读时长:5min」 正片 减少if...else面条代码 一旦当我们写到超过两个if...else的 ...
- oracle中查询表字段信息及主键字段
select a.owner, a.table_name, a.column_name, a.data_type, d.constraint_type, a.num_nulls from all_ta ...
- 威纶通作为modbusTcp主站应用
1.在威纶通中添加modbus Tcp,作为主机,在Tcp服务器客户端模型中作为客户端: 2.威纶通地址与modbus标准功能码的对应关系: 地址1x_single_Bit,对应02功能码(读输入线圈 ...