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,13,18,1,null,6],L = 6,R = 10
输出:23
注意:
树中的节点数最多为10000。
最终答案保证不到2^31。
02 第一种解法
既然给的是二叉搜索树,那么遍历节点我们就选取中序遍历的方式,这样最直接,存入List中,然后遍历List中的节点值,将节点值大于等于L且小于等于R的进行累加,最后返回sum即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
List<Integer> list = new ArrayList<Integer>();
helper(root, list);
int sum = 0;
for (Integer num : list) {
if (num >= L && num <= R) {
sum += num;
}
}
return sum;
}
public void helper(TreeNode root, List<Integer> list){
if (root == null) {
return ;
}
helper(root.left, list);
list.add(root.val);
helper(root.right, list);
}
}
03 第二种解法
针对第一种解法,我们也可以使用迭代的方式来实现,借助栈。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode tem = stack.pop();
if (tem != null) {
list.add(tem.val);
}
if (tem != null && tem.left != null) {
stack.push(tem.left);
}
if (tem != null && tem.right != null) {
stack.push(tem.right);
}
}
int sum = 0;
for (Integer num : list) {
if (num >= L && num <= R) {
sum += num;
}
}
return sum;
}
}
04 第三种解法
我们其实不用将节点值存起来后再统一处理,直接在遍历节点的时候,将在[L,R]范围内的节点值累加即可,最后返回sum。此解法是迭代的方式。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
int sum = 0;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode tem = stack.pop();
if (tem != null) {
if (tem.val >= L && tem.val <= R) {
sum += tem.val;
}
}
if (tem != null && tem.left != null) {
stack.push(tem.left);
}
if (tem != null && tem.right != null) {
stack.push(tem.right);
}
}
return sum;
}
}
05 第四种解法
针对第三种解法,我们也可以使用递归的方式。定义一个全局变量sum,依旧使用中序遍历的方式,将在[L,R]范围内的节点值累加,最后返回sum。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private Integer sum = 0;
public int rangeSumBST(TreeNode root, int L, int R) {
if (root == null) {
return sum;
}
rangeSumBST(root.left, L, R);
if (root.val >= L && root.val <= R) {
sum += root.val;
}
rangeSumBST(root.right, L, R);
return sum;
}
}
06 第五种解法
针对第四种解法,我们也可以不使用全局变量,借助二叉搜索树节点值按照左根右的大小排列特性,如果当前节点值比L小,就往右边找,如果比R大,就往左边找,最后求和。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
if (root == null) {
return 0;
}
if (root.val < L) {
return rangeSumBST(root.right, L, R);
}
if (root.val > R) {
return rangeSumBST(root.left, L, R);
}
return root.val + rangeSumBST(root.left, L, R) +
rangeSumBST(root.right, L, R);
}
}
07 小结
算法专题目前已连续日更超过七个月,算法题文章227+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)的更多相关文章
- [Swift]LeetCode938. 二叉搜索树的范围和 | Range Sum of BST
Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...
- 【python】Leetcode每日一题-二叉搜索树节点最小距离
[python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...
- Java实现 LeetCode 783 二叉搜索树节点最小距离(遍历)
783. 二叉搜索树节点最小距离 给定一个二叉搜索树的根节点 root,返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- LeetCode 96——不同的二叉搜索树
1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...
- 【JavaScript】Leetcode每日一题-二叉搜索树的范围和
[JavaScript]Leetcode每日一题-二叉搜索树的范围和 [题目描述] 给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和. 示例1: 输入: ...
- Leetcode 96. 不同的二叉搜索树
题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n ...
- LeetCode动画 | 1038. 从二叉搜索树到更大和树
今天分享一个LeetCode题,题号是1038,标题是:从二分搜索树到更大和数. 题目描述 给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等 ...
- [LeetCode]96. 不同的二叉搜索树(DP,卡特兰数)
题目 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ ...
随机推荐
- Kattis - bitwise Bitwise (RMQ+尺取+树上dfs)
题意:有一个长度为n的序列,让你把它分成k段,段内元素取or,段间取and,求能够得到的最大值. 这个算法是我和xz场上yy出来的,然而时间不够了没写出来,而且时间复杂度是$O(nlogn+nlogA ...
- 数组扩展运算符 -ES6
1.将数组转为以逗号分隔的序列 2.格式 ...[1,2,3 ] 3.若扩展运算符后面是一个空数组,则不产生效果[ ] 4.用于函数参数 function add(x,y) { console.log ...
- k8sCronJob控制器
CronJob用于管理job控制器资源的运行时间,job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但cronjob可以以类似于linux操作系统的周期性任务作业计划的方式控制其运行时间点 ...
- 阅读之MySQL数据库分表
移动互联网时代,海量的用户数据每天都在产生,基于用户使用数据等这样的分析,都需要依靠数据统计和分析,当数据量小时,数据库方面的优化显得不太重要,一旦数据量越来越大,系统响应会变慢,TPS直线下降,直至 ...
- hiho #1474 拆字游戏(dfs,记录状态)
#1474 : 拆字游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Kui喜欢把别人的名字拆开来,比如“螺”就可以拆成“虫田糸”,小Kui的语文学的不是很好,于是 ...
- C# WinForm捕获全局异常(捕获未处理的异常)
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- HTTP 请求出现 405 not allowed 的一种解决办法
问题:http post请求网页会出现405 原因: Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求 解决:将post请求改为get请求
- Qt 5 常用类及基本函数
//主要使用类#include <Qstring> #include <QMenu> #include <QMenuBar> #include <QToolB ...
- Python实用黑科技——找出序列里面出现次数最多的元素
需求: 如何从一个序列中快速获取出现次数最多的元素. 方法: 利用collections.Counter类可以解决这个问题,特别是他的most_common()方法更是处理此问题的最快途径.比如,现在 ...
- Android_(游戏)打飞机04:绘画敌机、添加子弹
(游戏)打飞机01:前言 传送门 (游戏)打飞机02:游戏背景滚动 传送门 (游戏)打飞机03:控制玩家飞机 传送门 (游戏)打飞机04:绘画敌机.添加子弹 传送门 (游戏)打飞机05:处理子弹, ...