Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution to problem 938 of leetcode #429

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions C++/RangeSumOfBST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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;
}
};

// 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ 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 | |

<br/>
<div align="right">
Expand Down