leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
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的更多相关文章
- 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 ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
随机推荐
- ❀❀ 名词解释(PM、PD、UE、UI、RD....)
PM:产品经理,项目负责人.一个产品,首先由PM来分析细分市场.目标客户的诉求,规划产品的卖点.杀手级应用,这个过程通常PD已经介入了,这个层面上,商业问题.业务逻辑的流畅是思考的焦点. 举凡产品从创 ...
- 阻塞队列BlockingQueue之LinkedBlokingQueue
1.简介 LinkedBlokingQueue 是链表实现的有界阻塞队列,此队列的默认和最大长度为 Integer.MAX_VALUE.此队列按照先进先出的原则对元素进行排序.ArrayList和Ar ...
- [人物存档]【AI少女】【捏脸数据】人物鉴赏190
AISChaF_20200201011129905.png AISChaF_20200123004135233.png
- 每天进步一点点------创建Microblaze软核(三)
第七步 进入SDK开发环境编译完成后弹出如下对话框,选择SDK的工作目录.在MicroblazeTutor中创建一个Workspace文件夹,并选择该文件夹为SDK的工作目录.进入SDK主界面.第八步 ...
- ES6常用语法,面试应急专用!
常用的ES6语法 注:该文章为转载,原地址为https://www.jianshu.com/p/fb019d7e8b15 什么是ES6? ECMAScript 6(以下简称ES6)是JavaScr ...
- 【做题笔记】洛谷P1036 选数
作为一个 DFS 初学者这题真的做得很惨...其实窝学 DFS 一年多了,然后一开始就学不会最近被图论和数据结构打自闭后才准备好好学一学233 一开始,直接套框架,于是就有 #include < ...
- 微信小程序 获取cookie 以及设置 cookie
小程序开发中我们需要获取到后端给的cookie进行请求验证,但是微信并没有帮我们保存cookie,那么我们要维持会话需要自己来保存cookie,并且请求的时候加上cookie 1.获取cookie 在 ...
- iview渲染函数
<Table border :columns="discountColumns" :data="discountData.rows"></Ta ...
- 利用ansible-playbook一键部署ELK(ElasticSearch,logstash and kibana)
一.部署前环境介绍: es集群5台(es01,es02,es03,es04,es05),logstash服务器1台(logstash2),kibana服务器1台(kibana2),模拟apache服务 ...
- Form DataGridView绑定BindingSource的几种方式
本文链接:https://blog.csdn.net/qq_15138169/article/details/83341076 在WinForm的开发中,ListView和DataGridView应用 ...