235.236:Lowest Common Ancestor of a Binary Search Tree/Tree(LCA问题)

JHU ICBM DTI Atlas纤维束模板介绍 JHU ICBM DTI atlas 纤维束的定位与功能介绍 阅读详情

连续两道LCA问题,一道是BST树的LCA求解,另外一道是普通二叉树的LCA求解。

235:

Total Accepted: 119152
Total Submissions: 311598
Difficulty: Easy
Contributors: Admin
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 v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______
       /              \
    ___2__          ___8__
   /      \        /      \
   0      _4       7       9
         /  \
         3   5
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

该题直接秒杀,递归版本:

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL || p == NULL || q == NULL)
            return NULL;
        if(p->val < root->val && q->val < root->val)
            return lowestCommonAncestor(root->left, p, q);
        else if(p->val > root->val && q->val > root->val)
            return lowestCommonAncestor(root->right, p, q);
        else
            return root;
    }
};

非递归版本:

/**
 * 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(p == NULL || q == NULL)
            return NULL;
        while(root){
            if(p->val < root->val && q->val < root->val)
                root = root->left;
            else if(p->val > root->val && q->val > root->val)
                root = root->right;
            else
                break;
        }
        return root;
    }
};

236:

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL)
            return NULL;
        if(p == root || q == root)
            return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if(left != NULL && right != NULL)
            return root;
        else if(left != NULL)
            return left;
        else
            return right;
    }
};

同样采用递归,不过这次不能利用BST的特性了。我们分别在查找某个节点的所有节点看它的子节点是否有p或q即可。

化工设备应力分析软件:STAAD.Pro二次开发_(24).非线性分析技术 在STAAD.Pro 中,可以通过命令。 阅读详情

相关推荐

codesys工程ST语言学习笔记(二)写文件操作与添加库方法

ST语言文件读写功能 由于实验中需要大量的数据,文件读写比较方便,但是网上的文件读写的程序很多都用不了,小编将自己的工程发布出来

sf9090的博客 1万+

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

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

我是要成为海贼王的男人! 229

B站充电视频下载器(需配合会员Cookie使用,仅供学习交流,Python)

这个程序是一个用于下载B站充电视频的工具,依赖于用户提供的会员Cookies。

qq_40753021的博客 2万+

235. Lowest Common Ancestor of a Binary Search Tree&236. Lowest Common Ancestor of a Binary Tree

问题描述235.Lowest Common Ancestor of a Binary Search TreeGiven 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 Wikiped

Bryan要加油 416

MODNet模型轻量化实战:从PyTorch到NCNN移动端部署,模型缩小75%性能不减

本文详细介绍了MODNet模型轻量化实战,从PyTorch到NCNN移动端部署的全过程。通过量化压缩和推理优化,模型体积缩小75%的同时保持性能不减,在千元级安卓设备上实现25FPS的实时人像抠图。文章深入解析MODNet架构、PyTorch到ONNX转换的陷阱与解决方案,以及NCNN量化部署的极致优化实践,为移动端实时人像抠图提供了技术突破与工程实现。

weixin_29098117的博客 302

保姆级教程:用MATLAB搞定双目相机标定,从导入图片到参数解读全流程

本文提供了一份详细的MATLAB双目相机标定全流程指南,涵盖从图片准备到参数解读的每个环节。重点讲解了棋盘格选择、拍摄技巧、MATLAB工具箱设置及参数优化,帮助读者避免常见错误并提升标定精度。特别适合机器人导航、三维重建等领域的开发者。

weixin_33737134的博客 437

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 thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between ...

weixin_30408739的博客 121

LeRobot 机器人学习(Robot Learning)入门教程(一)

本文介绍了现代机器人学习领域的发展现状与关键技术。随着机器学习进步和大规模机器人数据的普及,机器人技术正从传统基于模型的方法转向数据驱动的学习范式。HuggingFace开发的开源库lerobot实现了端到端集成,支持多种机器人平台和先进算法(强化学习、行为克隆等)。核心创新包括LeRobotDataset标准化格式,统一处理多模态时间序列数据,并支持高效批处理和流式访问。教程涵盖经典机器人学基础、强化学习原理、模仿学习技术和通用模型开发,结合代码示例展示如何构建适应多样化任务的机器人系统。该资源旨在为研究

2506_90492529的博客 2010

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree

思路 由于是BST,思路相比与236题有所简化。 只需要判断2个目标是否都在同一分支即可。 递归实现 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int...

我要上岸的博客 136

已解决“TimeouEror [WinEror 10060由于连接方在一段时间后没有正确答复 或连接的主机没有反应,连接尝试失败“,亲测有效!!!

已解决"TimeouEror [WinEror 10060由于连接方在一段时间后没有正确答复 或连接的主机没有反应,连接尝试失败",亲测有效!!!

wbajsjhhhhh的博客 1万+

ROS从入门到精通2-7:Gazebo仿真之动态生成障碍物

动态生成障碍物在机器人导航、碰撞避障算法测试、多机协同等领域应用广泛,可以帮助开发人员快速测试,本文介绍该技术并提供基于ROS Gazebo的实现

FRIGIDWINTER的博客 4912

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

基于 51 单片机的智能手势控制小车设计与实现

智能手势控制小车以STC89C52RC 单片机为控制核心,通过手势识别模块采集用户手势指令,经单片机处理后驱动电机执行前进、后退、左转、右转等动作。该项目融合了传感器技术、单片机编程和电机驱动技术,具有成本低、易实现、互动性强的特点,适合作为嵌入式系统入门实践案例。本项目成功实现了基于 51 单片机的智能手势控制小车,通过 APDS-9960 模块采集手势信号,经单片机处理后驱动电机完成基本动作,验证了 “传感器 + 单片机 + 执行器” 的嵌入式系统设计思路。

weixin_42300449的博客 186

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

xinqrs01的博客 1015

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

leetcode 235, 236. 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 t...

蓝羽飞鸟的博客 189

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

235/236. Lowest Common Ancestor of a Binary Search Tree/Lowest Common Ancestor of a Binary Tree

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 Wikipedi

Nick 267

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

侠客小虎的博客 215

235. Lowest Common Ancestor of a Binary Search Tree && 236. Lowest Common Ancestor of a Binary 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 de...

weixin_30480583的博客 75

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

[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的专栏 547

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

98. Validate Binary Search Tree(判断合法二叉搜索树)

Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key. The right

FreeeLinux's blog 1707

532. K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in

FreeeLinux's blog 1193

257. Binary Tree Paths(打印二叉树所有路径)

Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are:["1->2->5", "1->3"]首先我采用了dfs,但是我的方法有点麻烦。方法一:dfs/**

FreeeLinux's blog 883
上一篇: 单源最短路径复习--Dijkstra算法和Floyd算法
下一篇: 200. Number of Islands+130. Surrounded Regions(并查集/DFS)
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址