当前位置:网站首页>剑指Offer 68 - II. 二叉树的最近公共祖先
剑指Offer 68 - II. 二叉树的最近公共祖先
2022-07-04 22:20:00 【LuZhouShiLi】
剑指 Offer 68 - II. 二叉树的最近公共祖先
题目
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
思路
- 如果P和Q都是root的左右节点,那么root就是我们要找的最近公共祖先
- 如果P和Q都是root的左节点,那么返回lowestCommonAncestor(root.left,p,q)
- 如果P和Q都是root的右节点,那么返回lowestCommonAncestor(root.right,p,q)
边界条件讨论:
- 如果root = null 说明找不到,如果root = p或者root = q,返回root
- 如果左子树没找到,递归函数返回null,证明p和q都是在同测,那么就在右子树中查找
- 如果右子树没找到,递归函数返回null,证明p和q都是在同测,那么就在左子树中查找
代码
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q)
{
return root;
}
TreeNode leftNode = lowestCommonAncestor(root.left,p,q);
TreeNode rightNode = lowestCommonAncestor(root.right,p,q);
if(leftNode == null)
{
return rightNode;
}
if(rightNode == null)
{
return leftNode;
}
return root;
}
}
边栏推荐
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- Logo special training camp Section IV importance of font design
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
- Concurrent optimization summary
- LOGO特训营 第四节 字体设计的重要性
- Why is Dameng data called the "first share" of domestic databases?
- 10 schemes to ensure interface data security
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- 啃下大骨头——排序(二)
猜你喜欢
MySQL Architecture - logical architecture
攻防世界 MISC 进阶 glance-50
Close system call analysis - Performance Optimization
攻防世界 MISC 进阶区 3-11
攻防世界 MISC 高手进阶区 001 normal_png
Common open source codeless testing tools
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
SPSS installation and activation tutorial (including network disk link)
达梦数据凭什么被称为国产数据库“第一股”?
随机推荐
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
Persistence mechanism of redis
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Close system call analysis - Performance Optimization
Introduction and application of bigfilter global transaction anti duplication component
Logo special training camp Section V font structure and common design techniques
攻防世界 MISC 高手进阶区 001 normal_png
华泰证券是国家认可的券商吗?开户安不安全?
[acwing] solution of the 58th weekly match
Solana chain application crema was shut down due to hacker attacks
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Analog rocker controlled steering gear
Why is Dameng data called the "first share" of domestic databases?
Breakpoint debugging under vs2019 c release
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
SPSS installation and activation tutorial (including network disk link)
Challenges faced by virtual human industry