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

【图像超分】论文精读:Residual Dense Network for Image Super-Resolution(RDN) 一个非常深的卷积神经网络 (CNN) 最近在图像分类 (SR) 方面取得了巨大成功,并提供了分层特征。然而,大多数基于深度 CNN 的 SR 模型没有充分利用原始低分辨率 (LR) 图像中的层次特征,从而实现相对较低的性能。在本文中,我们提出了一种新的残差密集网络(RDN)来解决图像SR中的这个问题。我们充分利用所有卷积层的分层特征。具体来说,我们提出了残差密集块(RDB)通过密集连接的卷积层提取丰富的局部特征。RDB 进一步允许将前一个 RDB 的状态与当前 RDB 的所有层直接连接,从而产生连续的内存 阅读详情

连续两道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即可。

[毕设项目-大模型微调]现代文翻译文言文【基于LLaMA3.1-8B微调】 教你基于LLaMA3.1-8B微调如何做到现代文翻译文言文 阅读详情

相关推荐

ThinkPad X230魔改指南:从博通网卡驱动到1080P屏显全攻略(附Sonoma避坑清单)

本文详细介绍了ThinkPad X230的全面改造方案,包括博通网卡驱动(BCM94352HMB)的安装与调试、1080P屏幕改装技术以及macOS Sonoma系统的适配优化。通过硬件升级和软件调优,使这台经典商务本焕发新生,适合DIY爱好者和技术极客参考实践。

weixin_30379531的博客 589

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

华为eNSP与VMware虚拟机联动实战:从零搭建仿真网络环境

本文详细介绍了如何从零开始搭建华为eNSP与VMware虚拟机的联动仿真网络环境。通过环境准备、网络适配器配置、eNSP与VMware网络集成、安全策略与NAT配置等步骤,帮助读者掌握构建企业级网络实验场景的关键技术。这种组合特别适合网络工程师进阶训练和华为认证考试准备,实现低成本、高灵活性的网络仿真实验。

weixin_30415113的博客 504

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要加油 412

奇迹MU单机版开服全流程:从零搭建到分辨率调整(附GM工具使用技巧)

本文详细介绍了奇迹MU单机版从零搭建到分辨率调整的完整流程,包括环境配置、服务端启动、GM工具使用技巧等关键步骤。特别针对现代高分辨率显示器提供了优化方案,帮助玩家轻松搭建个性化游戏环境,重温经典MMORPG的独特魅力。

b3n4m5q6w7的博客 685

MATLAB信号处理实战:VMD分解参数调优全指南(附避坑技巧)

本文深入探讨了MATLAB中变分模态分解(VMD)的核心参数调优实战技巧。针对模态数K和惩罚系数α的选择难题,提供了从经验估计、可视化观察到量化指标辅助的渐进式调优框架,并分享了避免过分解与欠分解的实用避坑指南,旨在帮助工程师高效处理非平稳信号,提取清晰的物理特征。

weixin_29079671的博客 146

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

Vue3 与 Bpmn-js:打造现代化 BPMN 流程设计器

随着业务流程的复杂化,流程设计器成为企业级应用中不可或缺的一部分。BPMN(Business Process Model and Notation)是一种标准的业务流程建模语言,广泛应用于业务流程管理。本文将介绍如何使用 Vue3 和 Bpmn-js 打造一个现代化的 BPMN 流程设计器。

九天的专栏 2120

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

详解AXI4-Lite接口(3)--AXI4-Lite接口IP源码仿真及分析(Master接口)

通过本文您会学到:1、了解AXI4-Lite_Master接口的逻辑实现;2、加深Xilinx IP、仿真工具的使用;3、提高阅读时序图的能力。

孤独的单刀 6448

KEPServerEX物联网网关(IoT Gateway)REST客户端代理(非原创)

(中文版)资料来源于【OPC-KEPServerEX学习KEPWare交流】905485143群的【455196383】【零下一度】 一。KEPServerEX物联网网关REST客户端代理演示 在KSE中,首先构建一个带有通道/设备/标签的模拟器系统。 例如,下面是三个随机项目。 直接到IoT网关插件,添加新代理。 从类型列表中选择REST Client。 终结点...

-雲飛兄-博客 5810

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

MATLAB应用实战系列(四十三)-基于MATLAB的光伏并网系统仿真设计

在Matlab中建立光伏电站接入系统模型,包括光伏发电逆变器及负荷模型等,仿真分析接入点处的电能质量,实现高品质并网运行。 壹 构建太阳能电池模型。 初始化参数如下图所示。 贰 构建MPPT控制模型。 具体封装如下所示: 通过PWM算法实现DC-DC稳步输出最大点。 叁 三、构建直流短距离输电线路。 仿真模型如下图: 通过电容来均衡线路电压,使得线路电压保持可靠性。 肆 通过逆变器变换,使得直流线路转换成...

wenyusuran的专栏 3173

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

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

蓝羽飞鸟的博客 186

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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的博客 74

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

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 1191

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、付费专栏及课程。

余额充值