当前位置:网站首页>leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
2022-07-07 10:30:00 【涛涛英语学不进去】
235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
- 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
- 输出: 6
- 解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
- 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
- 输出: 2
- 解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
本题用我上一节的通用做法也能做,不过是二叉搜索树,有序,可以针对性质来做。 性质:有序(非递减)
package com.programmercarl.tree;
/** * @ClassName BstLowestCommonAncestor * @Descriotion TODO * @Author nitaotao * @Date 2022/7/6 12:36 * @Version 1.0 * https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/ * 235. 二叉搜索树的最近公共祖先 **/
public class BstLowestCommonAncestor {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//因为是有序的,只要找到最接近两个元素的结点
// 模板结点在当前结点左边
if (root.val > p.val && root.val > q.val) {
root = root.left;
} else if (root.val < p.val && root.val < q.val) {
root = root.right;
} else {
//root在两个结点位置之间了,是闭区间。而且是 [p,q]的闭区间,不是 p 就是 q
//因为是一步步位移的,所以是临界点
return root;
}
return lowestCommonAncestor(root, p, q);
}
}
边栏推荐
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- Solutions to cross domain problems
- 数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
- BGP actual network configuration
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- On valuation model (II): PE index II - PE band
- PowerShell cs-utf-16le code goes online
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
- Static comprehensive experiment
- 小红书微服务框架及治理等云原生业务架构演进案例
猜你喜欢
College entrance examination composition, high-frequency mention of science and Technology
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
On valuation model (II): PE index II - PE band
Static routing assignment of network reachable and telent connections
SQL head injection -- injection principle and essence
跨域问题解决方案
[pytorch practice] image description -- let neural network read pictures and tell stories
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
随机推荐
Is it safe to open an account in Ping An Securities mobile bank?
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
源代码防泄密中的技术区别再哪里
Experiment with a web server that configures its own content
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
Tutorial on principles and applications of database system (009) -- conceptual model and data model
问题:先后键入字符串和字符,结果发生冲突
EPP+DIS学习之路(1)——Hello world!
OSPF exercise Report
sql-lab (54-65)
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
(待会删)yyds,付费搞来的学术资源,请低调使用!
密码学系列之:在线证书状态协议OCSP详解
浅谈估值模型 (二): PE指标II——PE Band
SQL Lab (46~53) (continuous update later) order by injection
@Bean与@Component用在同一个类上,会怎么样?
30. Feed shot named entity recognition with self describing networks reading notes
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题