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 | | +
⬆️ Back to Top From 6d8c858c5f41cc7249e4beb4da5bfebb2fd9bf49 Mon Sep 17 00:00:00 2001 From: Niyati Soni <85472780+Niyati3031@users.noreply.github.com> Date: Mon, 2 Oct 2023 10:08:21 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4a1535d1..b050f798 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,6 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Javascript](./JavaScript/98.Validate-Binary-Search-Tree.js) | _O(log(n))_ | _O(log(n))_ | Medium | Binary Tree | | 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 | |
From d07899f8d0ccd6c4464dfb079058de40b528e4d3 Mon Sep 17 00:00:00 2001 From: Niyati Soni <85472780+Niyati3031@users.noreply.github.com> Date: Mon, 2 Oct 2023 10:12:20 +0530 Subject: [PATCH 4/4] Update RangeSumOfBST.cpp --- C++/RangeSumOfBST.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/C++/RangeSumOfBST.cpp b/C++/RangeSumOfBST.cpp index 64860921..72f865c7 100644 --- a/C++/RangeSumOfBST.cpp +++ b/C++/RangeSumOfBST.cpp @@ -34,3 +34,18 @@ class Solution { return sum; } }; + +// Test Case 1 +// Input root = [10,5,15,3,7,null,18] +// low = 7 +// high = 15 +// Excepted = 32 +// Output = 32 + + +// Test Case 2 +// Input root = [10,5,15,3,7,13,18,1,null,6] +// low = 6 +// high = 10 +// Expected = 23 +// Output = 23