leetcode 235, 236. Lowest Common Ancestor of a Binary Search Tree(二叉树的最低共同祖先)

Win11此电脑怎么放在桌面上?Win11此电脑如何显示在电脑桌面上? 相信用习惯Win10的用户都喜欢电脑电脑桌面上的此电脑,但是很多用户更新完Win11正式版以后,发现桌面只有回收站和Microsoft Edge图标,没有此电脑。那么,Win11此电脑要如何显示呢?下面小编为大家简单介绍一下操作方法!5、桌面图标设置窗口,点击勾选计算机,然后点击确定,桌面就可以显示此电脑图标了。1、桌面空白处,点击右键,在打开的菜单项中,选择显示设置;4、个性化 - 主题,相关设置下,点击桌面图标设置;3、右侧点击主题(安装、创建、管理);2、设置窗口,左侧点击个性化; 阅读详情

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given 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
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

给出一棵二叉树(235是二叉搜索树)和两个节点,找出两个节点的最低共同祖先,节点自己也是自己的祖先

思路:
最简单的情况,p或q就是root,或者root是null,那么返回root就行
如果p和q分别在左子树和右子树,则返回它们的root
依次搜索左子树和右子树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) {
            return root;
        }
        
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left == null) {
            return right;
        }
        
        if (right == null) {
            return left;
        }
        
        return root;
    }

如果上面这个不好理解,
可以根据BST的特点,左子树的值 < root < 右子树的值,
当然本题p, q本身也可以是root,所以是<=
一旦出现了p.val <= root.val <= q.val 或者 q.val <= root.val <= p.val
就说明root就是要求的最小祖先。
用前序遍历即可。

class Solution {
    TreeNode node;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        preTraversal(root, p, q);
        return node;
    }
    
    boolean preTraversal(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return false;
        
        
        if((p.val <= root.val && q.val >= root.val) || 
           (q.val <= root.val && p.val >= root.val)) {
            node = root;
            return true;
        }
        if(preTraversal(root.left, p, q)) return true;
        if(preTraversal(root.right, p, q)) return true;
        return false;
    }
}
【图论】系列(一)之图论的构成 图论是研究图的数学分支,本章介绍了图论的基本构成和概念 阅读详情

相关推荐

【学习笔记】一文搞懂IEPE传感器

本文介绍了IEPE(集成电路压电式)传感器的工作原理及其在振动监测中的应用。通过压电效应原理,IEPE传感器将机械振动转换为电信号,内置电荷放大器将高阻抗电荷信号转换为低阻抗电压信号输出。还介绍了传感器的三轴测量结构、4mA恒流源供电机制以及灵敏度与量程的关系,并通过风扇实验演示了故障诊断过程。实验使用USB-3123采集卡和SRD-1104信号调理器,展示了正常与异常状态下的振动数据差异,验证了IEPE传感器在设备状态监测中的有效性。

m0_62969865的博客 2823

LeetCode每日一题(Lowest Common Ancestor of a Binary Search Tree)

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodespandqas the lowest node inTthat has both...

wangjun861205的博客 248

SkyWalking跨线程Trace传递实战:手把手教你用RunnableWrapper解决异步链路中断问题

本文深入解析了SkyWalking在异步编程场景下链路追踪中断的根源,并提供了使用RunnableWrapper等官方包装类的实战解决方案。通过详细的原理解析和代码示例,手把手指导开发者如何实现跨线程的Trace传递,确保分布式系统监控链路的完整性,有效解决异步任务导致的链路中断问题。

sand8的博客 496

leetcode-235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined b...

别说话写代码的博客 436

大模型驱动时序量化因子自动优化:从WorldQuant回测到实战策略

本文探讨了如何利用大模型驱动时序量化因子的自动优化,构建从创意生成到实战验证的完整流程。通过结合WorldQuant回测平台,搭建自动化流水线,详细拆解了因子生成、回测验证与迭代优化的核心模块。文章分享了从初版实现到进阶优化的实践经验,并指出提示词工程与领域知识注入是关键,旨在实现人机智能协作,提升量化策略研发效率。

wdx012345的博客 804

Windows安装WSL子系统及docker,以及WSL和docker配置、使用及问题解决

在Windows操作系统中,Ubuntu子系统(也称为Windows Subsystem for Linux, WSL)为开发者提供了一个在Windows环境下运行Linux环境的平台。然而,有时用户在按照Ubuntu子系统或者使用WSL时,可能会遇到各种问题,下面总结一下解决方式。想要在Windows上安装Docker(实际上是基于Hyper-V或者WSL2这两项虚拟化技术),需要开启虚拟化功能,所以不管是对于系统还是硬件都有一定的要求。

sym的博客 9789

LeetCode235. Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between ...

李歇特冯-兹拜因巴哈的博客 216

10万+字超硬核 机器学习 笔记(面试八股)

10w字总结面试中可能用到的机器学习八股和常用算法,包括感知机(Perceptron)、多层感知机(MLP, Multi-Layer Perceptron)、支持向量机(SVM, Support Vector Machine)、K 最近邻(KNN, K-Nearest Neighbors)、朴素贝叶斯(Naive Bayes)、决策树(Decision Tree)、随机森林(Random Forest)、Bagging、Boosting、GBDT、XGBoos、LightGBM等。

haopinglianlian的博客 4665

235. Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined bet...

dici5041的博客 165

Lammps不同亲疏水性粗糙表面的润湿行为模拟(包含in文件)

Lammps粗糙表面的润湿行为模拟(包含in文件)前言—润湿行为模拟一、物理模型构建二、具体in文件—in文件内循环亲疏水性三、具体in文件—分子模板构建初始模型以及平衡态模拟四、全部代码(基地粗糙表面只提供data文件) 前言—润湿行为模拟 润湿现象:当液体与固体接触时,液体的附着层将沿固体表面延伸。当接触角 θ\rm \thetaθ 为锐角时,液体润湿固体,若θ为零时,液体将展延到全部固体表面上。润湿现象的产生与液体和固体的性质有关。(来自百度百科) 润湿行为的本质是固液之间的相互作用,也就是分子间的范

Rockyoung 7095

用Python爬虫抓取大宗交易成交数据:全面解析与实战代码

大宗交易是指在证券市场中,涉及大额买卖的交易。此类交易通常由机构投资者进行,其交易量较大,对市场的影响显著。大宗交易数据包含了买卖双方的成交价格、成交量等关键信息,这些数据对于投资者和分析师而言,具有重要的市场预测意义。Python爬虫是一种自动化抓取互联网上数据的工具。通过爬虫,我们可以定期获取大宗交易数据,节省人工查询的时间。爬虫的工作原理通常包括:发送HTTP请求、获取响应数据、解析网页内容、提取关键信息、存储和展示数据。

2201_76125261的博客 417

leetcode 235. Lowest Common Ancestor of a Binary Search Tree

解题思路: 注意到已知条件是二叉搜索树,即左子树节点的值小于根节点的值,根节点的值小于右子树节点的值。 如果两个节点位于根节点的两侧,根节点就是最小祖先节点;如果两个节点都小于根节点,递归判断根节点的左子节点;如果两个节点都大于根节点,递归判断根节点的右子节点。原题目:Given a binary search tree (BST), find the lowest common ancesto

永远不要给自己找借口 418

ThingsBoard PE专业版解决方案技术文档——温度&湿度

温湿度解决方案

weixin_67440240的博客 820

leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binar...

https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二叉搜索树的性质:左子树所有节点小于根节点,右子树所有节点大于根节点 如果两个节点的最大值小于根节点,那最低公共祖先一定在左子树,去左子树找; 如果两个节点的最小值大于根节点,那最低公共祖先一定...

weixin_30951231的博客 83

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

leetcode之二叉树类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree

leetcode之深搜递归回溯类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree

u010246947的专栏 452

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode.235(236) Lowest Common Ancestor of a Binary Search Tree && II

题目235:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw

侠客小虎的博客 213

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree

Problem DescriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is

Narcssus的专栏 334

leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树BST的最近公共祖先LCA + 深度优先遍历DFS

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between

JackZhangNJU的专栏 544

Leetcode 235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree Total Accepted:77021Total Submissions:205265Difficulty:Easy Given a binary search tree (BST), find the lowest common ancestor (L...

weixin_30591551的博客 70

LeetCode235:Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between ...

卷卷萌的博客 234

Lowest Common Ancestor of a Binary Search Tree & a Binary Tree

235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description 题目大意:给定一棵二叉查找树和两个节点p和q,要求返回这两个节点的第一个公共祖先。 思路:对...

weixin_30484739的博客 79

【算法拾光】队列|在每个树⾏中找最⼤值 + 最后⼀块⽯头的重量

本文总结两道经典算法题:一是通过层序遍历(BFS)逐层找最大值,利用队列分层处理,每层维护最大值;二是用大根堆(优先队列)模拟石头碰撞过程,每次取最大两块石头,差值重新入堆,直至剩余一块或无石。前者适用于树的层次遍历,后者适合动态获取最值场景。核心思想:队列用于分层遍历,堆用于高效取极值。

wuqingyigexiaoba的博客 202

把 KV Cache 压到 FP4:BitDecoding-FP4 的算法-系统协同设计复盘

长上下文推理卡在KV Cache的显存与带宽上,而现有FP4方案要么精度崩、要么速度废。这篇文章复盘BitDecoding-FP4如何用三个算法补丁加一个系统重构,在B300上把服务吞吐提升2.4–3.0倍,精度几乎不掉。

helloagent的博客 319

【算法拾光】链表 哈希表|K个一组翻转链表 + 两数之和

K个一组翻转链表需分段反转,借助哑节点与前驱指针维护连接,避免断链;两数之和利用哈希表实现O(1)查找,空间换时间,高效定位目标组合。前者考察链表指针操作,后者聚焦映射关系构建,均为面试高频题。

wuqingyigexiaoba的博客 341

排序算法深度笔记(下):非比较类排序的线性突破与工程边界

排序算法时间复杂度额外空间复杂度稳定性是否基于比较选择排序O(N^2)(O(1))不稳定是冒泡排序O(N^2)(O(1))稳定是插入排序O(N^2)(O(1))稳定是归并排序O(N\log N)(O(N))稳定是快速排序O(N\log N)O(log N)不稳定是堆排序O(N\log N)O(1)不稳定是计数排序(O(N + M))O(M))稳定否基数排序O(N)稳定否桶排序平均O(N) / 最坏O(N^2)

2401_87069538的博客 617

优选算法:分治-归并排序

交易逆序对的总数

Wangshijie1015的博客 154
上一篇: leetcode 240. Search a 2D Matrix II (搜索二维矩阵)
下一篇: leetcode 125. Valid Palindrome (有效回文字符串)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值