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. 这道 ...
随机推荐
- Day5 - 05 函数的参数-关键字参数
可变参数可以传入任意个参数,并在函数调用时自动组为一个tuple,而关键字参数允许传入任意个携带参数名的参数,这些关键字参数在函数内部自动组为一个dict. >>> ...
- Day5 - 04 函数的参数-可变参数*
传入的参数的个数是可变的. 例子:定义一个函数,通过给出一组数,返回这组数中最大值与最小值的和. def msum(numbers): r = max(numbers) + min ...
- 图像处理论文详解 | Deformable Convolutional Networks | CVPR | 2017
文章转自同一作者的微信公众号:[机器学习炼丹术] 论文名称:"Deformable Convolutional Networks" 论文链接:https://arxiv.org/a ...
- Linux IO/NFS tunning 性能优化及检测
Linux IO/NFS tunning:IO Test=======dd 测试读性能的命令# time dd if=/nfsfolder/testfile of=/dev/null bs=1kdd ...
- Linux 修改权限,查看进程,结束进程 命令
在linux终端先输入ll,可以看到bai如:-rwx-r--r-- (一共10个参数) 表示文件所属组和du用户的对应权限.zhi第一个跟dao参数属于管理员,跟chmod无关,先不管.2-4参数: ...
- NET Core 使用EF Core的Code First迁移和DBFirst
DBFirst (1)Microsoft.EntityFrameworkCore (2)Microsoft.EntityFrameworkCore.Design (3)Microsoft.Entity ...
- Flutter低版本迁移到高版本
记录一次Flutter v1.12.13+hotfix.8 升级到1.22.4(当前最新) 后 ,旧项目的适配过程 工具:Android Studio 4.1.1 1.android 的MainAct ...
- RxJava +Retrofit 简单使用
1.添加依赖 compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter ...
- 毕业三年,如何达到月薪30K?我想跟你聊聊!!
写在前面 很多读者私信问我,自己工作三多年了,随着工作年限的不断增长,感觉自己的技术水平与自己的工作年限严重不符.想跳槽出去换个新环境吧,又感觉自己的能力达不到心仪公司的标准,即使投了简历也没人来通知 ...
- PLA-机器学习基石2
转自:http://blog.csdn.net/u013455341/article/details/46747343 在<机器学习基石>这门课里面也进入了第一讲的内容,这次学习到的是Pe ...