二叉搜索树的范围和

LeetCode-938

  1. 首先需要仔细理解题目的意思:找出所有节点值在L和R之间的数的和。
  2. 这里采用递归来完成,主要需要注意二叉搜索树的性质。
/**
* 给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和。
* 二叉搜索树保证具有唯一的值。
**/
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
10
/ \
5 15
/ \ \
3 7 18
**/
class Solution {
private:
int sum=0;//
int l,r;
public:
void rangeSum(TreeNode* node){
//cout<<node->val<<endl;
if(node->val<=r&&node->val>=l){
sum+=node->val;
if(node->left)
rangeSum(node->left);
if(node->right)
rangeSum(node->right);
}else if(node->val<l){
if(node->right)
rangeSum(node->right);
}else if(node->val>r){
if(node->left)
rangeSum(node->left);
}
}
int rangeSumBST(TreeNode* root, int L, int R) {
this->l=L;
this->r=R;
if(root)
rangeSum(root);
return sum;
}
};
int main(){
TreeNode* t1=new TreeNode(10);
TreeNode* t2=new TreeNode(5);
TreeNode* t3=new TreeNode(15);
TreeNode* t4=new TreeNode(3);
TreeNode* t5=new TreeNode(7);
TreeNode* t6=new TreeNode(18);
t2->left=t4;t2->right=t5;
t3->left=t6;
t1->left=t2;t1->right=t3;
Solution solution;
cout<<solution.rangeSumBST(t1,7,15)<<endl;
system("pause");
return 0;
}

LeetCode-二叉搜索树的范围和的更多相关文章

  1. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

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

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

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

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

  6. [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. 这道 ...

  7. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  8. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  10. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. 计蒜客-A1139 dfs

    在一个 n \times mn×m 的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地图上 ...

  2. vs2019激活码

    Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...

  3. C++构造函数、复制函数易错点

    C++中复制函数在三种情况下自动调用: 用一个对象初始化另一个对象 函数的参数为对象 函数的返回值为对象 下面用几个代码片段解释复制函数的调用中的一些常见"坑": 一:默认复制函数 ...

  4. Pyqt5使用

    一.帮助文档 二.PyQt5库结构 三. 面向对象的编程模式 class Windows(QWidget): def __init__(self): #继承父类的QWidget的方法 super(). ...

  5. CS144学习(1)Lab 0: networking warmup

    CS144的实验就是要实现一个用户态TCP协议,对于提升C++的水平以及更加深入学习计算机网络还是有很大帮助的. 第一个Lab是环境配置和热身,环境按照文档里的配置就行了,前面两个小实验就是按照步骤来 ...

  6. 蓝桥杯-摔手机问题【dp】

    非常详细的题解:戳这里 例题:poj-3783 Balls Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 115 ...

  7. codeforces 858A

    A. k-rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. sqll-libs(3)

    单引号测试,最外层单引号错误信息near ''1'') LIMIT 0,1' at line 1 由此我们可以确定SQL后台语句为select * from table where id =('inp ...

  9. translate.js

    http://www.openxrest.com/translatejs/index.html translate.js translate.js is a jQuery plugin to tran ...

  10. React hox

    React hox https://github.com/umijs/hox