-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution938.cs
44 lines (37 loc) · 947 Bytes
/
Solution938.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
33
34
35
36
37
38
39
40
41
42
43
44
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution938
{
/// <summary>
/// 938. Range Sum of BST - Easy
/// <a href="https://leetcode.com/problems/range-sum-of-bst">See the problem</a>
/// </summary>
public int RangeSumBST(TreeNode root, int low, int high)
{
int sum = 0;
var stack = new Stack<TreeNode>();
stack.Push(root);
while (stack.Count > 0)
{
var node = stack.Pop();
if (node == null)
{
continue;
}
if (node.val > low)
{
stack.Push(node.left);
}
if (node.val < high)
{
stack.Push(node.right);
}
if (node.val >= low && node.val <= high)
{
sum += node.val;
}
}
return sum;
}
}