当前位置:网站首页>Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
2022-07-07 12:47:00 【Taotao can't learn English】
235. The nearest common ancestor of a binary search tree
Given a binary search tree , Find the nearest common ancestor of the two specified nodes in the tree .
In Baidu Encyclopedia, the most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, Recently, the common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
for example , Given the following binary search tree : root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
- Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
- Output : 6
- explain : node 2 And nodes 8 The most recent public ancestor of 6.
Example 2:
- Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
- Output : 2
- explain : node 2 And nodes 4 The most recent public ancestor of 2, Because by definition, the nearest common ancestor node can be the node itself .
This problem can also be done by using the general method in my last section , It's just a binary search tree , Orderly , It can be done according to the nature . nature : Orderly ( The decreasing )
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. The nearest common ancestor of a binary search tree **/
public class BstLowestCommonAncestor {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// Because it's orderly , Just find the node closest to the two elements
// The template node is on the left of the current node
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 Between the two nodes , It's a closed interval . And it's [p,q] The closed interval of , No p Namely q
// Because it moves step by step , So it's the critical point
return root;
}
return lowestCommonAncestor(root, p, q);
}
}
边栏推荐
- About IPSec
- SSM框架搭建的步骤
- Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
- 如何将 @Transactional 事务注解运用到炉火纯青?
- 静态Vxlan 配置
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- SQL lab 1~10 summary (subsequent continuous update)
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- Airserver automatically receives multi screen projection or cross device projection
猜你喜欢
【深度学习】图像多标签分类任务,百度PaddleClas
NPM instal reports agent or network problems
Zhimei creative website exercise
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
The IDM server response shows that you do not have permission to download the solution tutorial
Day-15 common APIs and exception mechanisms
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
Routing strategy of multi-point republication [Huawei]
【统计学习方法】学习笔记——提升方法
Charles: four ways to modify the input parameters or return results of the interface
随机推荐
解密GD32 MCU产品家族,开发板该怎么选?
How to use PS link layer and shortcut keys, and how to do PS layer link
How much does it cost to develop a small program mall?
About sqli lab less-15 using or instead of and parsing
What if does not match your user account appears when submitting the code?
[learn micro services from 0] [02] move from single application to service
opencv的四个函数
【从 0 开始学微服务】【03】初探微服务架构
Common knowledge of one-dimensional array and two-dimensional array
Attack and defense world ----- summary of web knowledge points
Talk about four cluster schemes of redis cache, and their advantages and disadvantages
AirServer自动接收多画面投屏或者跨设备投屏
Airserver automatically receives multi screen projection or cross device projection
Day-18 hash table, generic
Experiment with a web server that configures its own content
有什么类方法或是函数可以查看某个项目的Laravel版本的?
leetcode刷题:二叉树20(二叉搜索树中的搜索)
[pytorch practice] image description -- let neural network read pictures and tell stories
SQL Lab (41~45) (continuous update later)
【统计学习方法】学习笔记——支持向量机(下)