当前位置:网站首页>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);
}
}

边栏推荐
- Decrypt gd32 MCU product family, how to choose the development board?
- PowerShell cs-utf-16le code goes online
- SQL Lab (46~53) (continuous update later) order by injection
- Static vxlan configuration
- Simple network configuration for equipment management
- Epp+dis learning path (1) -- Hello world!
- Processing strategy of message queue message loss and repeated message sending
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- ENSP MPLS layer 3 dedicated line
猜你喜欢

百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文

Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier

The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful

【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型

EPP+DIS学习之路(1)——Hello world!

消息队列消息丢失和消息重复发送的处理策略
![An error occurred when vscade tried to create a file in the target directory: access denied [resolved]](/img/14/9899f5a765872fb3238be4305a2dc7.png)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]

Static comprehensive experiment

zero-shot, one-shot和few-shot

解密GD32 MCU产品家族,开发板该怎么选?
随机推荐
Zhimei creative website exercise
@Bean与@Component用在同一个类上,会怎么样?
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
VSCode的学习使用
SQL head injection -- injection principle and essence
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
BGP actual network configuration
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
【统计学习方法】学习笔记——第五章:决策树
Hi3516 full system type burning tutorial
Up meta - Web3.0 world innovative meta universe financial agreement
What is an esp/msr partition and how to create an esp/msr partition
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
Apache installation problem: configure: error: APR not found Please read the documentation
Utiliser la pile pour convertir le binaire en décimal