Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created November 12, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/8818a1684ee7bb59abfb404ff6dd792e to your computer and use it in GitHub Desktop.
Save fpdjsns/8818a1684ee7bb59abfb404ff6dd792e to your computer and use it in GitHub Desktop.
[leetcode] 938. Range Sum of BST : https://leetcode.com/problems/range-sum-of-bst/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int rangeSumBST(TreeNode* root, int L, int R) {
if(root == NULL) return 0;
int ans = 0;
if(L <= root->val && root->val <= R) ans+=root->val;
if(root->val <= R) ans += rangeSumBST(root->right, L, R);
if(L <= root->val) ans += rangeSumBST(root->left, L, R);
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment