LeetCode938. 二叉搜索树的范围和
题目
1 class Solution {
2 public:
3 int sum = 0;
4 int rangeSumBST(TreeNode* root, int low, int high) {
5 dfs(root,low,high);
6 return sum;
7 }
8 void dfs(TreeNode* root,int low,int high){
9 if(root!=NULL){
10 dfs(root->left,low,high);
11 if(root->val >= low && root->val <= high)
12 sum += root->val;
13 dfs(root->right,low,high);
14 }
15 }
16 };
LeetCode938. 二叉搜索树的范围和的更多相关文章
- [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 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- 【Scrapy笔记】使用方法
安装: 1.pip install wheel 安装wheel 2.安装Twisted a.访问 http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted ...
- 【原创】WPF TreeView带连接线样式的优化(WinFrom风格)
一.前言 之前查找WPF相关资料的时候,发现国外网站有一个TreeView控件的样式,是WinFrom风格的,样式如下,文章链接:https://www.codeproject.com/tips/67 ...
- java_day03
一.this关键字的作用 昨天学了Java private 关键字 ,private关键字主要是为了 保护变量 ,感觉用着好像并不是特别方便 如果需要访问本类当中的成员变量,需要使用的格式: this ...
- 第 13 章 StringTable详解
目录 第 13 章 StringTable 1.String 的基本特性 1.1.String 概述 1.2.String 的基本特征 1.3.String 的底层结构 2.String 的内存分配 ...
- MyBatis史上最全文章
老规矩,本篇文章 不做 MyBatis 的 编码讲解 ,只介绍 文章学习的一些优秀文章 重点在于不要循规蹈矩,教程 这样走,你不一定要按他这样走,按自己的方式来,学习效率会更高,网上的教程有很多,今天 ...
- Vue必须必须要注意的几个细节
1.每次执行完,尽量npm run dev 一次,有时候又缓存问题 2.安装sass 一.使用save会在package.json中自动添加.因为sass-loader依赖于node-sass npm ...
- Ubuntu系统的ifconfig命令不能执行
新安装的Ubuntu想要用WinSCP传文件时发现,ifconfig命令用不了 ping www.baidu.com 获得回应,应该是ifconfig未安装 解决这个问题,首先如图(时间较长,获取:[ ...
- Mac 上使用 Shell 脚本 + adb shell 实现简单的 Android 模拟点击自动化测试
需求 在 A 界面,点击跳转到 B 界面(该界面会执行一些业务),再点击返回键出现 Dialog 弹窗,点击确认退出按钮,返回 A 界面.不断循环. 思路 一开始想到的就是按键精灵,下了 mac 版使 ...
- java判断路径是文件夹还是文件
当给定一个路径没有后缀的时候,很难分辨代码是文件还是文件夹,如下图: 我在桌面建立了一个名为one的文件,路径为:/Users/XXXXXX/Desktop/one java代码如下: import ...
- jsp文件导包
可以在一个页面中用上多个<% @ page %>指令,但是其中的属性只能用一次,不过也有个例外,那就是import属性.因为import属性和Java中的import语句差不多(参照Jav ...