forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0938-RangeSumOfBST.cs
32 lines (31 loc) · 1001 Bytes
/
0938-RangeSumOfBST.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//-----------------------------------------------------------------------------
// Runtime: 172ms
// Memory Usage: 44.3 MB
// Link: https://leetcode.com/submissions/detail/378634468/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class _0938_RangeSumOfBST
{
public int RangeSumBST(TreeNode root, int L, int R)
{
if (root == null)
return 0;
else if (root.val < L)
return RangeSumBST(root.right, L, R);
else if (root.val > R)
return RangeSumBST(root.left, L, R);
else
return root.val + RangeSumBST(root.left, L, R) + RangeSumBST(root.right, L, R);
}
}
}