From 63c35cfeedd88d96997b512604933539611fa580 Mon Sep 17 00:00:00 2001
From: Niyati Soni <85472780+Niyati3031@users.noreply.github.com>
Date: Mon, 2 Oct 2023 09:53:57 +0530
Subject: [PATCH 1/4] Create RangeSumOfBST.cpp
---
C++/RangeSumOfBST.cpp | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
create mode 100644 C++/RangeSumOfBST.cpp
diff --git a/C++/RangeSumOfBST.cpp b/C++/RangeSumOfBST.cpp
new file mode 100644
index 00000000..64860921
--- /dev/null
+++ b/C++/RangeSumOfBST.cpp
@@ -0,0 +1,36 @@
+// Question:- Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
+
+/**
+ * Definition for a binary tree node.
+ * struct TreeNode {
+ * int val;
+ * TreeNode *left;
+ * TreeNode *right;
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
+ * };
+ */
+
+class Solution {
+public:
+ void solve(TreeNode* root,int low,int high,int &ans){
+ if(!root) return;
+ if(root-> val >=low && root->val <=high){ //checking if root value is in range
+ ans+=root->val;
+ solve(root->left, low, high, ans); //recursively calling left tree
+ solve(root->right, low, high, ans); //recursively calling right tree
+ }
+ else if(root->val> high){ //if root value is more than high, then according to BST low values won't be in right tree so search left tree
+ solve(root->left, low, high, ans);
+ }
+ else if(root->val < low){ //if root value is less than low, then according to BST low values won't be in left tree so search right tree
+ solve(root->right, low, high, ans);
+ }
+ }
+ int rangeSumBST(TreeNode* root, int low, int high) {
+ int sum = 0;
+ solve(root, low, high, sum);
+ return sum;
+ }
+};
From a714d13af125258785e95bda765b490d43d3fddf Mon Sep 17 00:00:00 2001
From: Niyati Soni <85472780+Niyati3031@users.noreply.github.com>
Date: Mon, 2 Oct 2023 10:04:35 +0530
Subject: [PATCH 2/4] Update README.md
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index b2cac739..4a1535d1 100644
--- a/README.md
+++ b/README.md
@@ -261,6 +261,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Java](./Java/Redundant-Connection/redundant-connection.java) | _O(N)_ | _O(N)_ | Medium | Tree, Union Find |
| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) |[C++](./C++/Binary-Tree-Level-Order-Traversal.cpp)| _O(n)_ | _O(n)_ | Medium | Binary Tree, map | |
+| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) |[C++](./C++/RangeSumOfBST.cpp)| _O(n)_ | _O(n)_ | Easy | Binary Search Tree | |
+