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.
Example 1:
- Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
- Output: 32
Example 2:
- Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
- Output: 23
Note:
- The number of nodes in the tree is at most
10000
. - The final answer is guaranteed to be less than
2^31
.
Approach #1: C++. [recursive]
- /**
- * 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:
- int rangeSumBST(TreeNode* root, int L, int R) {
- ans = 0;
- dfs(root, L, R);
- return ans;
- }
- private:
- int ans;
- void dfs(TreeNode* root, int L, int R) {
- if (root != NULL) {
- if (L <= root->val && root->val <= R) {
- ans += root->val;
- }
- if (L < root->val) {
- dfs(root->left, L, R);
- }
- if (R > root->val) {
- dfs(root->right, L, R);
- }
- }
- }
- };
Approach #2: Java. [Iterative]
- /**
- * 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 ans = 0;
- Stack<TreeNode> stack = new Stack();
- stack.push(root);
- while (!stack.isEmpty()) {
- TreeNode node = stack.pop();
- if (node != null) {
- if (L <= node.val && node.val <= R)
- ans += node.val;
- if (L < node.val)
- stack.push(node.left);
- if (R > node.val)
- stack.push(node.right);
- }
- }
- return ans;
- }
- }
Approach #3: Python.
- # Definition for a binary tree node.
- # class TreeNode(object):
- # def __init__(self, x):
- # self.val = x
- # self.left = None
- # self.right = None
- class Solution(object):
- def rangeSumBST(self, root, L, R):
- """
- :type root: TreeNode
- :type L: int
- :type R: int
- :rtype: int
- """
- ans = 0
- stack = [root]
- while stack:
- node = stack.pop()
- if node:
- if L <= node.val <= R:
- ans += node.val
- if L < node.val:
- stack.append(node.left)
- if R > node.val:
- stack.append(node.right)
- return ans
938. Range Sum of BST的更多相关文章
- 【Leetcode_easy】938. Range Sum of BST
problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完
- 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- Leetcode 938. Range Sum of BST
import functools # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, ...
- LeetCode #938. Range Sum of BST 二叉搜索树的范围和
https://leetcode-cn.com/problems/range-sum-of-bst/ 二叉树中序遍历 二叉搜索树性质:一个节点大于所有其左子树的节点,小于其所有右子树的节点 /** * ...
- LeetCode--Jewels and Stones && Range Sum of BST (Easy)
771. Jewels and Stones (Easy)# You're given strings J representing the types of stones that are jewe ...
- LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)
这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938).给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有 ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- [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 ...
- Leetcode938. Range Sum of BST二叉搜索树的范围和
给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和. 二叉搜索树保证具有唯一的值. 示例 1: 输入:root = [10,5,15,3,7,null,18], L = 7 ...
随机推荐
- RANDOM 的用法
random 用法 1.利用RANDOM取随机数 shell有一个环境变量RANDOM,范围是0--32767 如果我们想要产生0-25范围内的数:$(($RANDOM%26),在$(()) 是可以省 ...
- PYTHON加密解密字符串
依赖包安装部分 安装依赖包: pip install pycryptodome 在你的python环境中的下图红框路径中找到 crypto 将其改成 Crypto 代码部分 #!/usr/bin/en ...
- vue 的基本语法
一 . Vue 的介绍 1 . 前端的三大框架 (可以去 GitHub 查看三个框架的 star 星) vue : 作者尤雨溪, 渐进式的JavaScript 框架 react : Faceb ...
- 如何浏览github上所有的公开的项目?
github 上面项目多如牛毛,没有维护的.没有意义的或太过偏门的项目也是数不胜数,所以直接按照字母或者更新顺序浏览实在没什么意义. 有一个做法是去 github 搜 awesome list,比如通 ...
- HTTP 401.1 - 未授权:登录失败
1 HTTP 401.1 - 未授权:登录失败 由于用户匿名访问使用的账号(默认是IUSR_机器名)被禁用,或者没有权限访问计算机,将造成用户无法访问. 解决方案: 1 打开IIS,右键站点,选 ...
- BZOJ3262 陌上花开 —— 三维偏序 CDQ分治
题目链接:https://vjudge.net/problem/HYSBZ-3262 3262: 陌上花开 Time Limit: 20 Sec Memory Limit: 256 MBSubmit ...
- python日期格式化符号
python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数( ...
- Storm worker 并行度等理解
Storm 调优是非常重要的, 仅次于写出正确的代码, 好在Storm官网上有关于worker executors tasks的介绍, http://storm.incubator.apache.or ...
- tensorflow实现svm iris二分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)
iris二分类 # Linear Support Vector Machine: Soft Margin # ---------------------------------- # # This f ...
- SpringBoot_Exception_01_No plugin found for prefix 'spring-boto' in the current project
一.异常现象 spingbott项目在eclipse中执行maven命令:spring-boot:run, 出现异常: No plugin found for prefix 'spring-boto' ...