Skip to content

Commit

Permalink
Time: 331 ms (16.71%), Space: 64.6 MB (60.79%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Dec 7, 2022
1 parent da8e7a0 commit 289cf11
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 0938-range-sum-of-bst/0938-range-sum-of-bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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:
int ans=0;
void solve(TreeNode* root, int low, int high)
{
if(root==NULL)return;
if(root->val>=low && root->val<=high)
ans+=root->val;
solve(root->left,low,high);
solve(root->right,low,high);
}
int rangeSumBST(TreeNode* root, int low, int high) {
solve(root,low,high);
return ans;
}
};

0 comments on commit 289cf11

Please sign in to comment.