Leetcode-938 Range Sum of BST(二叉搜索树的范围和)

 1 class Solution
 2 {
 3     public:
 4         int rangeSumBST(TreeNode* root, int L, int R)
 5         {
 6             int result = 0;
 7             if(root->left)
 8                 result += rangeSumBST(root->left, L, R);
 9             if(root->right)
10                 result += rangeSumBST(root->right, L, R);
11             
12             if(root->val>=L && root->val<=R)
13                 result += root->val;
14             
15             return result;
16         }
17 };

 

posted @ 2018-11-13 18:06  Asurudo  阅读(474)  评论(0编辑  收藏  举报