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

边栏推荐
- Completion report of communication software development and Application
- PowerShell cs-utf-16le code goes online
- 【PyTorch实战】用RNN写诗
- Hi3516全系统类型烧录教程
- SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- [pytorch practice] image description -- let neural network read pictures and tell stories
- idm服务器响应显示您没有权限下载解决教程
- Using stack to convert binary to decimal
猜你喜欢

Static vxlan configuration

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

Solutions to cross domain problems

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

小红书微服务框架及治理等云原生业务架构演进案例

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

Idea 2021 Chinese garbled code

<No. 9> 1805. Number of different integers in the string (simple)

Up meta - Web3.0 world innovative meta universe financial agreement
随机推荐
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Cenos openssh upgrade to version 8.4
什么是局域网域名?如何解析?
平安证券手机行开户安全吗?
<No. 8> 1816. Truncate sentences (simple)
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
Configure an encrypted web server
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
跨域问题解决方案
NGUI-UILabel
Idea 2021 Chinese garbled code
BGP actual network configuration
Decrypt gd32 MCU product family, how to choose the development board?
小红书微服务框架及治理等云原生业务架构演进案例
Will the filing free server affect the ranking and weight of the website?
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
H3C HCl MPLS layer 2 dedicated line experiment
Airserver automatically receives multi screen projection or cross device projection
盘点JS判断空对象的几大方法
<No. 9> 1805. Number of different integers in the string (simple)