当前位置:网站首页>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);
}
}
边栏推荐
- Hi3516全系统类型烧录教程
- Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
- Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
- 30. Feed shot named entity recognition with self describing networks reading notes
- SQL head injection -- injection principle and essence
- When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
- 普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
- Simple network configuration for equipment management
- Completion report of communication software development and Application
- Static comprehensive experiment
猜你喜欢
Completion report of communication software development and Application
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
What is an esp/msr partition and how to create an esp/msr partition
Attack and defense world - PWN learning notes
Epp+dis learning path (1) -- Hello world!
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
消息队列消息丢失和消息重复发送的处理策略
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
SQL Lab (46~53) (continuous update later) order by injection
随机推荐
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Niuke website
sql-lab (54-65)
How to understand the clothing industry chain and supply chain
@Bean与@Component用在同一个类上,会怎么样?
【统计学习方法】学习笔记——支持向量机(上)
Minimalist movie website
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
Tutorial on the principle and application of database system (011) -- relational database
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Static comprehensive experiment
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
<No. 9> 1805. 字符串中不同整数的数目 (简单)
利用栈来实现二进制转化为十进制
Up meta - Web3.0 world innovative meta universe financial agreement
GCC compilation error
源代码防泄密中的技术区别再哪里
MPLS experiment