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:

  1. Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
  2. Output: 32

Example 2:

  1. Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
  2. Output: 23

Note:

  1. The number of nodes in the tree is at most 10000.
  2. The final answer is guaranteed to be less than 2^31.
 

Approach #1: C++. [recursive]

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int rangeSumBST(TreeNode* root, int L, int R) {
  13. ans = 0;
  14. dfs(root, L, R);
  15. return ans;
  16. }
  17.  
  18. private:
  19. int ans;
  20.  
  21. void dfs(TreeNode* root, int L, int R) {
  22. if (root != NULL) {
  23. if (L <= root->val && root->val <= R) {
  24. ans += root->val;
  25. }
  26. if (L < root->val) {
  27. dfs(root->left, L, R);
  28. }
  29. if (R > root->val) {
  30. dfs(root->right, L, R);
  31. }
  32. }
  33. }
  34. };

  

Approach #2: Java. [Iterative]

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int rangeSumBST(TreeNode root, int L, int R) {
  12. int ans = 0;
  13. Stack<TreeNode> stack = new Stack();
  14. stack.push(root);
  15. while (!stack.isEmpty()) {
  16. TreeNode node = stack.pop();
  17. if (node != null) {
  18. if (L <= node.val && node.val <= R)
  19. ans += node.val;
  20. if (L < node.val)
  21. stack.push(node.left);
  22. if (R > node.val)
  23. stack.push(node.right);
  24. }
  25. }
  26. return ans;
  27. }
  28. }

  

Approach #3: Python.

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def rangeSumBST(self, root, L, R):
  10. """
  11. :type root: TreeNode
  12. :type L: int
  13. :type R: int
  14. :rtype: int
  15. """
  16. ans = 0
  17. stack = [root]
  18. while stack:
  19. node = stack.pop()
  20. if node:
  21. if L <= node.val <= R:
  22. ans += node.val
  23. if L < node.val:
  24. stack.append(node.left)
  25. if R > node.val:
  26. stack.append(node.right)
  27.  
  28. return ans

  

938. Range Sum of BST的更多相关文章

  1. 【Leetcode_easy】938. Range Sum of BST

    problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  3. Leetcode 938. Range Sum of BST

    import functools # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, ...

  4. LeetCode #938. Range Sum of BST 二叉搜索树的范围和

    https://leetcode-cn.com/problems/range-sum-of-bst/ 二叉树中序遍历 二叉搜索树性质:一个节点大于所有其左子树的节点,小于其所有右子树的节点 /** * ...

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

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

    这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938).给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有 ...

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

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

  9. Leetcode938. Range Sum of BST二叉搜索树的范围和

    给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和. 二叉搜索树保证具有唯一的值. 示例 1: 输入:root = [10,5,15,3,7,null,18], L = 7 ...

随机推荐

  1. RANDOM 的用法

    random 用法 1.利用RANDOM取随机数 shell有一个环境变量RANDOM,范围是0--32767 如果我们想要产生0-25范围内的数:$(($RANDOM%26),在$(()) 是可以省 ...

  2. PYTHON加密解密字符串

    依赖包安装部分 安装依赖包: pip install pycryptodome 在你的python环境中的下图红框路径中找到 crypto 将其改成 Crypto 代码部分 #!/usr/bin/en ...

  3. vue 的基本语法

    一 . Vue 的介绍 1 . 前端的三大框架 (可以去 GitHub 查看三个框架的 star 星) vue   :  作者尤雨溪, 渐进式的JavaScript 框架 react :  Faceb ...

  4. 如何浏览github上所有的公开的项目?

    github 上面项目多如牛毛,没有维护的.没有意义的或太过偏门的项目也是数不胜数,所以直接按照字母或者更新顺序浏览实在没什么意义. 有一个做法是去 github 搜 awesome list,比如通 ...

  5. HTTP 401.1 - 未授权:登录失败

    1 HTTP 401.1 - 未授权:登录失败 由于用户匿名访问使用的账号(默认是IUSR_机器名)被禁用,或者没有权限访问计算机,将造成用户无法访问.    解决方案: 1 打开IIS,右键站点,选 ...

  6. BZOJ3262 陌上花开 —— 三维偏序 CDQ分治

    题目链接:https://vjudge.net/problem/HYSBZ-3262 3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit ...

  7. python日期格式化符号

    python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数( ...

  8. Storm worker 并行度等理解

    Storm 调优是非常重要的, 仅次于写出正确的代码, 好在Storm官网上有关于worker executors tasks的介绍, http://storm.incubator.apache.or ...

  9. tensorflow实现svm iris二分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)

    iris二分类 # Linear Support Vector Machine: Soft Margin # ---------------------------------- # # This f ...

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