当前位置:网站首页>剑指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;
}
}
边栏推荐
- Analysis of environmental encryption technology
- PHP short video source code, thumb animation will float when you like it
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
- Tiktok actual combat ~ the number of comments is updated synchronously
- Locust performance test - environment construction and use
- 9 - 类
- 攻防世界 MISC 进阶区 3-11
- PMO: compare the sample efficiency of 25 molecular optimization methods
- 攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
- Persistence mechanism of redis
猜你喜欢

Concurrent network modular reading notes transfer

Domestic database chaos

安装人大金仓数据库

Close system call analysis - Performance Optimization

攻防世界 MISC 高手进阶区 001 normal_png

Attack and defense world misc advanced area Hong
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/65/b36ca239e06a67c01d1eb0b4648f71.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

Logo special training camp Section V font structure and common design techniques
![[acwing] solution of the 58th weekly match](/img/e3/fd2c0ffbc9c7ca8a71875882d6c71b.png)
[acwing] solution of the 58th weekly match
随机推荐
[cooking record] - stir fried 1000 pieces of green pepper
Mongodb aggregation operation summary
攻防世界 MISC 进阶区 hong
Postgresqlql advanced skills pivot table
Jvm-Sandbox-Repeater的部署
How diff are the contents of the same configuration item in different environments?
SPSS安装激活教程(包含网盘链接)
PostgreSQLSQL高级技巧透视表
【OpenGL】笔记二十九、抗锯齿(MSAA)
Sqlserver encrypts and decrypts data
LOGO特训营 第五节 字体结构与设计常用技法
Business is too busy. Is there really no reason to have time for automation?
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
将QA引入软件开发生命周期是工程师要遵循的最佳实践
攻防世界 MISC 进阶区 hit-the-core
How to send a reliable request before closing the page
SPSS installation and activation tutorial (including network disk link)
质量体系建设之路的分分合合
Attack and defense world misc advanced area Hong