LeetCode: 235. Lowest Common Ancestor of a Binary Search Tree

C# 异步编程详解(Task,async/await) 同步和异步主要用于修饰方法。当一个方法被调用时,调用者需要等待该方法执行完毕并返回才能继续执行,我们称这个方法是同步方法;当一个方法被调用时立即返回,并获取一个线程执行该方法内部的业务,调用者不用等待该方法执行完毕,我们称这个方法为异步方法。异步的好处在于非阻塞(调用线程不会暂停执行去等待子线程完成),因此我们把一些不需要立即使用结果、较耗时的任务设为异步执行,可以提高程序的运行效率。 阅读详情

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 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.

Note:

  • All of the nodes’ values will be unique.
  • p and q are different and both values will exist in the BST.

解题思路 —— 递归求解

设 x 为 p,q较大值节点,y 为 p,q较小值节点。则当遍历到 r 节点时,它们的位置关系有以下几种可能性:

  • “x 节点 是 r 节点且 y 节点是 x 节点的左子树的节点”或者“y节点 是 r 节点且 x 节点是 y 节点的右子树的节点”
    这种情况下,最近的公共祖先节点就是 x 节点或 y 节点。
    xy

  • y 节点和 x 节点分别是 r 节点的左/右子树的节点
    这种情况下,最近的公共祖先节点就是 r 节点。
    xy

  • x 节点和 y 节点都是 r 节点的左子树的节点
    这种情况下,最近的公共祖先节点在 r 节点的左子树中。
    xy

  • x 节点和 y 节点都是 r 节点的右子树的节点
    这种情况下,最近的公共祖先节点在 r 节点的右子树中。
    xy

AC 代码

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
   TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
       if(root == nullptr || p == nullptr || q == nullptr)
       {
           return nullptr;
       }
       
       int maxVal = max(p->val, q->val);
       int minVal = min(p->val, q->val);
       
       //  “`x` 节点 是 `r` 节点且 `y` 节点是 `x` 节点的左子树的节点”
       // 或者“`y`节点 是 `r` 节点且 `x` 节点是 `y` 节点的右子树的节点”
       if(root->val == p->val || root->val == q->val)
       {
           return root;
       }
       // `y` 节点和 `x` 节点分别是 `r` 节点的左/右子树的节点
       else if(root->val < maxVal && root->val > minVal)
       {
           return root;
       }
       // `x` 节点和 `y` 节点都是 `r` 节点的左子树的节点
       else if(root->val > maxVal)
       {
           return lowestCommonAncestor(root->left, p, q);
       }
       //  `x` 节点和 `y` 节点都是 `r` 节点的右子树的节点
       else //if(root->val < minVal)
       {
           return lowestCommonAncestor(root->right, p, q);
       }
   }
};
CTFShow萌新区全攻略:从密码破解到Web渗透的实战解析 本文为CTFShow萌新区新手提供从密码破解到Web渗透的完整实战攻略。文章深入解析了密码学、隐写术、杂项和Web安全四大核心方向,分享了实用的解题工具、思维方法和绕过技巧,旨在帮助初学者建立系统的安全竞赛解题体系,而不仅仅是提供一份题解(WP)。 阅读详情

相关推荐

hcaptcha无感验证码逆向实战:从接口分析到环境补全

本文详细解析了hcaptcha无感验证码的逆向实战过程。通过分析核心接口与加密逻辑,重点介绍了采用环境补全策略来模拟真实浏览器环境,从而绕过验证码检测。文章提供了从接口抓包、剖析hsw.js加密核心到使用Node.js搭建模拟环境的完整步骤,为自动化操作提供了一种稳健的解决方案。

weixin_29324401的博客 1188

leetcode 235: Lowest Common Ancestor of a Binary Search Tree

leetcode 235: Lowest Common Ancestor of a Binary Search Tree python, java, c++

西施豆腐渣 1万+

FPGA串口通信----uart(一)

通过PLL锁相环IP的使用,可以输入时钟进行分频,倍频,也可以产生多路时钟输出信号。外部提供晶振的原因:①Fpga电路大多数需要时序分析②晶振在Fpga内部占空间大,所以放置于外部PCB板上Uart异步传输速率不能很快的原因:考虑到工程实际问题,假如时钟周期大(传输速率慢的话),采取到数据电平变化的边沿时刻的概率会小很多,这样数据也很准确,通常uart适用于2M.

mvpkuku的博客 974

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 betwee

哈哈的个人专栏 2816

扣子(coze)实战|手把手教你搭建AI换装自动化工作流

摘要:本文介绍了使用Coze平台搭建AI换装工作流的详细教程。工作流包含模特图片上传、服装替换、任务提交和智能轮询等模块,通过change_model插件实现异步换装处理,并内置30秒循环查询机制。该工具支持自定义API密钥对接不同AI服务商,目前已上线共享空间并提供会员服务(59.8元/年)和VIP课程(499元/年),包含100+智能体和实战案例资源。完整体验地址见文中链接。

yueyehuguang的博客 1854

STM32 SAI接口实战:用CubeMX快速配置多通道音频采集(附DMA优化技巧)

本文详细介绍了如何使用STM32的SAI接口通过CubeMX快速配置多通道音频采集系统,并提供了DMA双缓冲优化技巧。内容涵盖TDM模式配置、时钟同步、DMA优化及常见问题排查,帮助开发者高效实现高速数据采集应用。

weixin_42549037的博客 233

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 betw

judadeshu的博客 281

PWN-shellcode

构造一段shellcode的作用就是为了在缓冲区溢出时将shellcode的地址覆盖掉正常的返回地址。 题目: 1Ch=16+12=28,+esp=32:偏移量是32 能溢出:100-0x1c-4=68字节 C伪代码: 理论基础 函数返回步骤 : 保存返回值:函数返回值保存在EAX寄存器 弹出当前栈帧,恢复上一个栈帧 a) ESP + 当前栈帧大小:堆栈平衡基础上,降低栈顶,回收当前栈帧空间...

MuMuLee_的博客 2094

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 betw...

makerjj的博客 278

【SEED Labs 2.0】V*N Tunneling Lab

本文为 SEED Labs 2.0 - V*N Tunneling Lab 的实验记录。本实验工作量较大,但难度不大,重点在于搞清是谁发给谁,走的什么路径。另外,本次实验由于网络中的设备较多,修改了环境变量方便分清哪个窗口是哪个设备。这样操作起来就方便多了。...

Chenyang的博客 6997

安全运营面试总结

答案可以在我的博客中查找 1.自我介绍 2.TCP/IP七层模式 3.端口号(常见协议 数据库) 4.Owasp top 10 5.渗透测试过程 6.漏扫工具 7.中间件漏洞 8.应急响应 9.项目 10.SQL注入类型 11.TCP 三次握手 四次挥手 12.你有什么要问我的 1.自我介绍 2.项目介绍 3.宽字节注入的原理 4.渗透流程 5....

qq_51577576的博客 4421

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 betw

y_d_f_qing2010的专栏 183

OpenClaw 性能调优五步法

本文介绍了优化OpenClaw性能的五步方法,将响应时间从3秒降至300毫秒。首先建立性能基线,分析模型加载、上下文检索等主要瓶颈。五步优化包括:1)启用模型缓存减少60%延迟;2)优化记忆检索策略降低25%延迟;3)并行化工具调用节省40%时间;4)智能选择轻量级模型减少50%推理时间;5)启用流式输出改善70%感知延迟。最终整体性能提升10倍,并建议建立持续监控机制。这些优化方法经过实战验证,适用于不同规模的OpenClaw部署场景。

刘一说的IT专栏 1456

LeetCode笔记:235. Lowest Common Ancestor of a Binary Search Tree

找出一个二叉查找树中两个节点的最低共同祖先

Minecraft 1122

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

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

leetcode[235]:Lowest Common Ancestor of a Binary Search Tree

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: “Th

hahubaba的博客 308

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Leetcode:235. Lowest Common Ancestor of a Binary Search Tree(JAVA)

【问题描述】 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 de

莫名的程序猿 343

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 between two nodes p and q as the lowest node in T that has

weixin_51401145的博客 131

Leetcode学习笔记:#235. Lowest Common Ancestor of a Binary Search Tree

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 ...

ccystewart的博客 185

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

1.题目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

沐沐余风的专栏 492

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 betw

734

LeetCode算法第7篇: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 be

lgd370323的专栏 422

leetcode:235. Lowest Common Ancestor of a Binary Search Tree

个人学习笔记:在二叉搜索树中寻找最低共同父节点 很简单的题目,直接贴代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), le

u010016196的专栏 393

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 betwee

a253664942的专栏 424
上一篇: LeetCode: 234. Palindrome Linked List
下一篇: LeetCode: 236. Lowest Common Ancestor of a Binary Tree
IT技术小密圈
博客等级 码龄12年 1385粉丝 322原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值