二叉搜索树的范围和

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. 【uva 1442】Cav(算法效率)

    题意:有一个由N个片段构成宽度的洞穴,已知洞顶 si 和洞底 pi 的高度,要求储存尽量多的燃料. 解法:O(n),分别从1到N和从N到1扫一遍,调整每个片段合法的最大高度,求出答案. 1 #incl ...

  2. Codeforces Round #501 (Div. 3) B. Obtaining the String (思维,字符串)

    题意:有两个字符串\(S\)和\(T\),判断\(T\)是否能由\(S\)通过交换某位置的相邻字符得到,如果满足,输出交换次数及每次交换的位置,否则输出\(-1\). 题解:首先判断不满足的情况,只有 ...

  3. Linux 搭建网站

    wget http://dl.wdlinux.cn/lanmp_laster.tar.gz tar zxvf lanmp_laster.tar.gz sh lanmp.sh https://www.w ...

  4. git忽略规则以及.gitignore文件不生效解决办法

    正文 Git忽略规则: #此为注释 – 内容被 Git 忽略 .sample # 忽略所有 .sample 结尾的文件 !lib.sample # 但 lib.sample 除外 /TODO # 仅仅 ...

  5. Java容器--2021面试题系列教程(附答案解析)--大白话解读--JavaPub版本

    Java容器--2021面试题系列教程(附答案解析)--大白话解读--JavaPub版本 前言 序言 再高大上的框架,也需要扎实的基础才能玩转,高频面试问题更是基础中的高频实战要点. 适合阅读人群 J ...

  6. POJ 2923 Relocation(状压DP)题解

    题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...

  7. string logo(字符画),website,html5,css3,atom ide

    1 <!DOCTYPE html> <!-- Powered by... _ _ ____. ______ ._______. _______ ___ ___ sssssssss \ ...

  8. Iterators & Generators in depth

    Iterators & Generators in depth https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/It ...

  9. how to fetch a group promise api in order with the returned resolved result

    how to fetch a group promise api in order with the returned resolved result promise 一组依次请求,generator ...

  10. svg rect to polygon points & points order bug

    svg rect to polygon points & points order bug https://codepen.io/xgqfrms/pen/vYOWjYr?editors=100 ...