当前位置:网站首页>剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
2022-07-04 22:20:00 【LuZhouShiLi】
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
题目
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
思路
- 当p,q都在root的右子树中,则遍历root.right
- 当p,q都在root的左子树,则遍历root.left
- 当p,q在root两边,说明找到了最近公共祖先
代码
/** * 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) {
while(root != null)
{
if(root.val < p.val && root.val < q.val)
{
// 字节点都比root值要小 说明pq在左子树 遍历左子树
root = root.right;
}
else if(root.val > p.val && root.val > q.val)
{
root = root.left;
}
else{
break;// p q在root左右两侧 说明root就是最近公共祖先 直接跳出
}
}
return root;
}
}
边栏推荐
- leetcode 72. Edit distance edit distance (medium)
- MySQL Architecture - logical architecture
- 通过Go语言创建CA与签发证书
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Attack and defense world misc advanced grace-50
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- Li Kou 98: verify binary search tree
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- Logo special training camp section III initial creative techniques
猜你喜欢

Erik baleog and Olaf, advanced area of misc in the attack and defense world

将QA引入软件开发生命周期是工程师要遵循的最佳实践

攻防世界 MISC 進階區 Erik-Baleog-and-Olaf

LOGO special training camp section I identification logo and Logo Design Ideas

2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri

Unity-VScode-Emmylua配置报错解决

Unity修仙手游 | lua动态滑动功能(3种源码具体实现)

SPSS安装激活教程(包含网盘链接)

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

攻防世界 MISC 进阶区 Ditf
随机推荐
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
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
Postgresqlql advanced skills pivot table
Logo special training camp section II collocation relationship between words and graphics
PHP short video source code, thumb animation will float when you like it
PostgreSQL服务端编程聚合和分组
leetcode 72. Edit Distance 编辑距离(中等)
PostgreSQLSQL高级技巧透视表
虚拟人产业面临的挑战
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
SQL中MAX与GREATEST的区别
Logo Camp d'entraînement section 3 techniques créatives initiales
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
php短视频源码,点赞时会有大拇指动画飘起
Microservices -- Opening
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
Gnawing down the big bone - sorting (II)
Concurrent optimization summary
Attack and defense world misc advanced area Hong
攻防世界 MISC 进阶 glance-50