leetcode 938. Range Sum of BST(二叉搜索树范围内的和)

Given the root node of a binary search tree, return the sum of values of all nodes with a value in the range [low, high].

Example 1:
在这里插入图片描述
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32

Constraints:
The number of nodes in the tree is in the range [1, 2 * 104].
1 <= Node.val <= 105
1 <= low <= high <= 105
All Node.val are unique.

把BST中在[low, high]范围内的节点求和

思路:
和669题类似,669是把范围外的修剪掉。

BST的特征是左子树节点值 < root值 < 右子树节点值
可用中序遍历实现,中序遍历是升序排列的,所以中间满足条件的节点值直接相加即可。

class Solution {
    int res = 0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        inOrder(root, low, high);
        return res;
    }

    void inOrder(TreeNode root, int low, int high) {
        if(root == null) return;
        inOrder(root.left, low, high);
        if(root.val >= low && root.val <= high) res += root.val;
        inOrder(root.right, low, high);
    }
}

如果root.val < low的话,那么整个左子树和root都pass,只需要返回对右子树求和的结果。同理root.val > high时,整个右子树和root都不考虑,只要返回对左子树求和的结果。
如果root.val在范围内,那把root.val加到结果,然后继续对它的左子树和右子树求和。
节点是null时返回null。
递归实现

public int rangeSumBST(TreeNode root, int low, int high) {
    if(root == null) return 0;
   
    if(root.val >= low && root.val <= high) {
        return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
    } else if(root.val < low) {
        return rangeSumBST(root.right, low, high);
    } 
    
    return rangeSumBST(root.left, low, high);
    
}
【目标检测】yoloV5算法详解 一、与yoloV4相比,yoloV5的改进 输入端:在模型训练阶段,使用了Mosaic数据增强、自适应锚框计算、自适应图片缩放 基准网络:使用了FOCUS结构和CSP结构 Neck网络:在Backbone和最后的Head输出层之间插入FPN_PAN结构 Head输出层:训练时的损失函数GIOU_Loss,预测筛选框的DIOU_nns 二、yoloV5结构框架 CBL:CBL模块是由Conv+BN+Leaky_relu激活函数组成 Res unit:借鉴ResNet中的残差结构,用来构建深层网络,CBM是 阅读详情

相关推荐

电商产品组合策略:引流品与利润品的实战方法论

在电商运营中,产品组合策略是提升流量转化与利润增长的核心技术。其基本原理是通过数据化选品构建产品矩阵,利用引流品吸引用户流量,再通过利润品实现商业变现。这种策略不仅能优化广告投放的ROAS(广告投入产出比),还能显著提升客单价。技术实现上需要结合Google Analytics等工具进行归因分析,并运用动态库存管理模型控制风险。该方案特别适用于Amazon、Shopify等电商平台,能有效解决跨境电商业内流量获取与利润创造的平衡难题。通过实战案例可见,合理的产品组合可使客单价提升30-50%,是电商运营必备

weixin_42522009的博客 385

LeetCode 938. Range Sum of BST 求二叉搜索树的区间和

一、题目 Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. 二、代码 /** *...

可控的事情要谨慎,不可控的事情要乐观。 212

Matlab实战:用DQN算法让机器人学会自主避障(附完整代码)

本文详细介绍了如何使用Matlab实现DQN(深度Q网络)算法,使机器人在复杂环境中学会自主避障。通过环境建模、网络架构设计、奖励函数优化及实战调试,提供了完整的代码实现和训练策略,帮助开发者快速应用于机器人路径规划项目。

t8u9v0w1x的博客 238

12 Range Sum of BST

题目 Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Inp...

weixin_43367550的博客 275

Unity手游优化实战:用GPU Instancing把草地渲染的DrawCall从8000降到20(附完整Shader)

本文详细介绍了Unity手游中利用GPU Instancing技术优化草地渲染的实战方法,将DrawCall从8000降至20。通过Shader改造和性能优化技巧,开发者可以在移动端实现高效渲染,显著提升游戏性能。文章包含完整的Shader代码和优化策略,适合Unity开发者学习应用。

weixin_33691817的博客 502

国产系统迁移实战:在银河麒麟V10上搞定Wine32位支持,让老Windows软件跑起来

本文详细介绍了在银河麒麟V10操作系统上构建Wine32位支持的全过程,帮助用户解决Windows应用迁移难题。从系统架构差异分析到Wine环境配置,再到实战应用部署与优化,提供了一套完整的解决方案,确保老Windows软件在国产系统上稳定运行。

weixin_32288959的博客 673

【LeetCode】938. Range Sum of BST 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归日期 题目地址:https://leetcode.com/contest/weekly-contest-110/problems/range-sum-of-bst/ 题目描述 Given the root node of a binary search ...

负雪明烛 4547

7.DJI-PSDK:psdk负载应用固件升级(基于RTOS)

在单片机应用程序开发阶段,我们可以借助keil5和J-LINK来直接下载应用程序进行开发和调试, 但在产品交付之后,我们对应用程序做了改动和升级,是断然不可能采用这种方式的,我们应该将新版本的程序固件打包发给客户, 能够方便用户通过DJI Assistant2选择更新负载设备的固件,导入本地固件程序包,更新负载设备。使用STM32F4 开发的负载设备从Flash 中直接读取固件文件并运行负载设备程序,因此开发者需先烧录Bootloader 再烧录应用固件,烧录Bootloader 的步骤不再赘述。

哒宰的博客 683

938. Range Sum of BST

938. Range Sum of BST Medium Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have ...

snailrunfast的博客 https://segmentfault.com/u/snailrunfast 319

【WLAN】【调试】再谈如何使用Wireshark解密WiFi数据包——使用TK信息

- 本文介绍了一种wireshark使用TK信息解密空口报文的方式,该方式对是否有关联过程的报文没有要求。

一介草民,只为生活。 1212

【知识图谱】实践篇——基于知识图谱的《红楼梦》人物关系可视化及问答系统实践:part2知识获取与图谱构建、服务搭建

前序文章:其中原项目提供了关系数据如下: 其中五列数据表示:人物1,人物2,人物1与人物2关系,人物1所属家族,人物2所属家族。关系说明:以第一列为例,贾演是贾代化的父亲。下面我们就将这个数据放到neo4j中。这些需要去连接,需要安装这个包: 程序中,我使用去读取配置文件, 其中为了保持配置全局唯一性,使用单例的写法。 后面就是创建构建关系文件,构建neo4j图谱数据了,如下: 后期可能会将构建图谱内容使用flask去生成命令行的方式构建,暂时在开发的过程查看构建情况,便于调试。导入关系后,我们再查看

让算法融入生活,改变生活! 4093

LeetCode 938. Range Sum of BST

[Med] LeetCode 938. Range Sum of BST 链接: https://leetcode.com/problems/range-sum-of-bst/ 题目描述: Given the root node of a binary search tree, return the sum of values of all nodes with value between L ...

MrJustin的博客 238

英飞凌TC3xx之一起认识GTM系列(四)如何实现GTM与GPIO关联的配置(ATOM/TIM实例)

本文详细描述了针对英飞凌TC3xx芯片,如何运用GTM与GPIO之间的连接性,根据实际应用的需要对TOM和TIM相关寄存器进行配置。

无所适从的我的博客 3550

LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)

这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938)。给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有节点的值的总和。二叉搜索树的节点值唯一。例如: 输入:root = [10,5,15,3,7,null,18],L = 7,R = 15 输出:32 输入:root = [10,5,15,3,7,1...

小川的博客 310

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode //C - 938. Range Sum of BST

Summary: The problem requires summing the values of nodes within a given range [low, high] in a binary search tree (BST). The solution leverages the BST property to optimize traversal. If a node's value is below low, only the right subtree is explored (lef

Made in Code 627

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

[Leetcode][Python][DFS] 938. Range Sum of BST

[Leetcode][Python][DFS] 938. Range Sum of BST题目大意题目解析代码 题目大意 给定root二叉搜索树的节点,返回范围为[low, high] 的所有节点的值之和。 例题: 输入: root = [10,5,15,3,7,null,18],low = 7,high = 15 输出: 7+10+15 = 32 题目解析 第一点:利用BTS性质,node.left.val < node.val < node.right.val 第二点:DFS 第三点:递归

TwinkleZhao的博客 230

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[leetcode]938. Range Sum of BST(Python)

[leetcode]938. Range Sum of BST题目描述题目理解解题思路递归MyMethodTime 题目描述 Category:Easy Tree Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclus...

La_Ragazza的博客 370

leetcode 938. Range Sum of BST (BST某个范围内的和)

题目要求 给定一颗二叉搜索树,返回摸个范围内节点的和,包括L,R。其中BST中的值都是唯一的。 解题思路 对BST进行前序遍历,结果就是从小到大的顺序,所以,我们只要在遍历的时候,同时记录[L,R]范围内的数值和即可。 主要代码c++ class Solution { public: int sum = 0; int rangeSumBST(TreeNode* root, int...

爱喝舒化奶的博客 289

Leetcode 938.二叉搜索树的范围和(Range Sum of BST)

Leetcode 938.二叉搜索树的范围和 1 题目描述(Leetcode题目链接)   给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和。二叉搜索树保证具有唯一的值。 输入:root = [10,5,15,3,7,null,18], L = 7, R = 15 输出:32 输入:root = [10,5,15,3,7,13,18,1,null,6], L = 6...

qq_39378221的博客 211

938. Range Sum of BST*

938. Range Sum of BST* https://leetcode.com/problems/range-sum-of-bst/ 题目描述 Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). T...

珍妮的选择的博客 293

[LeetCode] 938. Range Sum of BST 二叉搜索树的区间和

Given the rootnode of a binary search tree, return the sum of values of all nodes with valu...

azl397985856的专栏 245

[leetcode] 938. Range Sum of BST

Description Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Input: root = [10,5,15,3,7,null,18], L = 7, R = 15

Learning from the mistakes 205

[LeetCode] 938. Range Sum of BST

Description Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. ...

dfojc86648的专栏 78

LeetCode - 938 - 二叉搜索树的范围和(range-sum-of-bst)

一 目录 不折腾的前端,和咸鱼有什么区别目录一 目录二 前言三 解题及测试四 LeetCode Submit五 解题思路六 进一步思考二 前言 难度:简单涉及知识:树、递归题目地址:ht...

weixin_41806099的博客 212
上一篇: leetcode 934. Shortest Bridge(最短桥梁)
下一篇: leetcode 947. Most Stones Removed with Same Row or Column(去掉同行列最多的石头)
蓝羽飞鸟
博客等级 码龄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、付费专栏及课程。

余额充值
⌂ 换网址