-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0938_Range_sum_of_BST_Test.cs
More file actions
84 lines (71 loc) · 2.31 KB
/
_0938_Range_sum_of_BST_Test.cs
File metadata and controls
84 lines (71 loc) · 2.31 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Solution._0938.Range_sum_of_BST;
using Common;
namespace _0938.Range_sum_of_BST.Tests
{
[TestClass()]
public class _0938_Range_sum_of_BST_Test
{
_0938_Range_sum_of_BST solution = new _0938_Range_sum_of_BST();
[TestMethod()]
public void RangeSumBST_Test1()
{
// Arrange
TreeNode root = AddNode(new string[] { "10", "5", "15", "3", "7", null, "18" });
int low = 7;
int high = 15;
var expected = 32;
// Act
var actual = solution.RangeSumBST(root, low, high);
// Assert
Assert.AreEqual(expected, actual);
}
[TestMethod()]
public void RangeSumBST_Test2()
{
// Arrange
TreeNode root = AddNode(new string[] { "10", "5", "15", "3", "7", "13", "18", "1", null, "6" });
int low = 6;
int high = 10;
var expected = 23;
// Act
var actual = solution.RangeSumBST(root, low, high);
// Assert
Assert.AreEqual(expected, actual);
}
private TreeNode AddNode(string[] items)
{
TreeNode root = new TreeNode(int.Parse(items[0].ToString()));
TreeNode current = root;
for (int i = 1; i < items.Length; i++)
{
if (items[i] == null) continue;
int num = int.Parse(items[i].ToString());
while (true)
{
if (num < current.val)
{
if (current.left == null)
{
current.left = new TreeNode(num);
break;
}
else
current = current.left;
}
else
{
if (current.right == null)
{
current.right = new TreeNode(num);
break;
}
else
current = current.right;
}
}
}
return root;
}
}
}