[ 命名建議 ] LeetCode 938. Range Sum of BST #108
Answered
by
twy30
LPenny-github
asked this question in
Q&A
-
|
原 issue: LeetCode: 再麻煩 @twy30 給予命名建議 orz 感激不盡 public static int RangeSumBST(TreeNode root, int low, int high)
{
var rootNode = root;
int inclusiveMin = low;
int inclusiveMax = high;
if (rootNode == null) { return 0; }
if (rootNode.val < inclusiveMin)
{ return RangeSumBST(rootNode.right, inclusiveMin, inclusiveMax); }
else if (rootNode.val > inclusiveMax)
{ return RangeSumBST(rootNode.left, inclusiveMin, inclusiveMax); }
return rootNode.val
+ RangeSumBST(rootNode.right, inclusiveMin, inclusiveMax)
+ RangeSumBST(rootNode.left, inclusiveMin, inclusiveMax);
} |
Beta Was this translation helpful? Give feedback.
Answered by
twy30
Dec 24, 2020
Replies: 2 comments 3 replies
-
|
你好 😊 int inclusiveMin = low;
int inclusiveMax = high;這裡可以考慮用
會更貼近 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LPenny-github
-
|
@twy30 附上熱騰騰的測試,有任何建議都歡迎。非常感謝 orz using Sandbox;
using Xunit;
namespace Tests
{
public class UnitTestRangeSumOfBST
{
[Fact]
public void LeetCodeExample1()
{
var node18 = new RangeSumOfBST.TreeNode(18);
var node7 = new RangeSumOfBST.TreeNode(7);
var node3 = new RangeSumOfBST.TreeNode(3);
var node15 = new RangeSumOfBST.TreeNode(15, null, node18);
var node5 = new RangeSumOfBST.TreeNode(5, node3, node7);
var node10 = new RangeSumOfBST.TreeNode(10, node5, node15);
Assert.Equal(32, new RangeSumOfBST().rangeSumBST(node10, 7, 15));
}
[Fact]
public void LeetCodeExample2()
{
var node1 = new RangeSumOfBST.TreeNode(1);
var node6 = new RangeSumOfBST.TreeNode(6);
var node3 = new RangeSumOfBST.TreeNode(3, node1, null);
var node7 = new RangeSumOfBST.TreeNode(7, node6, null);
var node13 = new RangeSumOfBST.TreeNode(13);
var node18 = new RangeSumOfBST.TreeNode(18);
var node5 = new RangeSumOfBST.TreeNode(5, node3, node7);
var node15 = new RangeSumOfBST.TreeNode(15, node13, node18);
var node10 = new RangeSumOfBST.TreeNode(10, node5, node15);
Assert.Equal(23, new RangeSumOfBST().rangeSumBST(node10, 6, 10));
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LPenny-github
你好 😊
這裡可以考慮用
inclusiveLowerBoundinclusiveUpperBound會更貼近
low,high的本意 🤔