Created
November 12, 2018 11:50
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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