1.原题:

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

The binary search tree is guaranteed to have unique values.

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

翻译:给一个BST,输出L和R之间所有的节点值的和。

2.解题思路:

这道题其实只是挂着个Binary search tree的羊头,卖的是递归的狗肉(也可以用迭代去做,但是我感觉出题者主要是想考察你递归的能力)。

其实题的意思很简单,就是让你找这个root里面所有大于L,小于R的值(包括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; }
int sum = 0;
if (root.val > L) { sum += rangeSumBST(root.left, L, R); }
if (root.val < R) { sum += rangeSumBST(root.right, L, R); }
if (root.val >= L && root.val <= R) { sum += root.val; }
return sum;
}
}

推荐阅读:

递归和迭代的区别:https://www.jianshu.com/p/32bcc45efd32

还有递归的概念

leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  2. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  3. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  4. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  5. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  6. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  7. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

  8. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

  9. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

随机推荐

  1. 阿里巴巴手册之-Arrays.asList()数组转集合的问题

    转载来源:https://blog.csdn.net/qq_36443497/article/details/79663663?utm_source=blogxgwz9 在使用工具类Arrays.as ...

  2. python 批量编译 批量删除

    把项目的py文件变异成pyc文件,好处是可以保护源码不泄露. 假如一个工程文件夹有1000个py文件,这个时候怎样快速处理 ? 两步走: ①  py--->pyc python -m compi ...

  3. 启动docker报Failed to start Docker Application Container Engine.解决

    [root@docker ~]# systemctl status docker.service● docker.service - Docker Application Container Engi ...

  4. kali 安装google输入法

    脑子一热装了一礼拜的kali,在20多遍的重装后终于成功了 先码一篇如何安装google输入法 首先得更新源,用leafpad /etc/apt/sources.list打开,或vi也可以,更新源百度 ...

  5. 通过scrapy,从模拟登录开始爬取知乎的问答数据

    这篇文章将讲解如何爬取知乎上面的问答数据. 首先,我们需要知道,想要爬取知乎上面的数据,第一步肯定是登录,所以我们先介绍一下模拟登录: 先说一下我的思路: 1.首先我们需要控制登录的入口,重写star ...

  6. Qt5 error LNK2019 无法解析的外部符号的解决办法

    今天在使用Qt Create 4.5.2时遇到一个莫名其妙的问题: 在原有工程里添加一个新类(有界面的),在调用的mainwindow.cpp中添加#include "a.h",然 ...

  7. linux下修改mysql的编码格式

    修改编码格式:https://blog.csdn.net/qq_30038111/article/details/79376137         改编码格式在配置文件中修改才有效,在命令行中修改没效 ...

  8. [爬坑日记] 安卓模拟器1903蓝屏 没开hyper-v

    先说解决方案: 我在升级完1903之后短短几个小时蓝屏了两次 还是不同原因,不由得开始怀疑这个版本的稳定性,随后发现只有启动安卓模拟器的时候必然蓝屏(还有一次蓝屏实在安装驱动的时候) 经过百度得知需要 ...

  9. 转载UUID.randomUUID()

    UUID.randomUUID()生成唯一识别码 原创 清晨-阳光zx 最后发布于2019-04-11 20:54:40 阅读数 3039 收藏 发布于2019-04-11 20:54:40 分类专栏 ...

  10. Ubuntu系统备份还原教程

    一.备份 很多人有备份系统的习惯,以防系统挂.Windows下可以用DISM创建一个系统镜像,在Ubuntu下,我们可以使用squashfs-tools创建系统镜像. 准备工作 可启动LiveCD一份 ...