-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Daily Coding Problem #135 [Easy]. 2024-11-30.
Find a minimum path sum from root to a leaf in a binary tree.
- Loading branch information
1 parent
61ba239
commit 9f0a070
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
DailyCodingProblem/src/DailyCodingProblem.Challenges/Y2024/M11/Day30/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace DailyCodingProblem.Challenges.Y2024.M11.Day30; | ||
|
||
public static class Solution | ||
{ | ||
public static int FindMinPathSum(TreeNode? node) | ||
{ | ||
if (node == null) | ||
{ | ||
return 0; | ||
} | ||
|
||
return Math.Min(FindMinPathSum(node.Left), FindMinPathSum(node.Right)) + node.Value; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
DailyCodingProblem/src/DailyCodingProblem.Challenges/Y2024/M11/Day30/TreeNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace DailyCodingProblem.Challenges.Y2024.M11.Day30; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public record class TreeNode | ||
{ | ||
public int Value { get; init; } | ||
public TreeNode? Left { get; init; } | ||
public TreeNode? Right { get; init; } | ||
} |
15 changes: 15 additions & 0 deletions
15
...CodingProblem/src/DailyCodingProblem.Challenges/Y2024/M11/Day30/_Description.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Find a min path sum in a binary tree | ||
|
||
Given a binary tree, find a minimum path sum from root to a leaf. | ||
|
||
For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15. | ||
|
||
10 | ||
/ \ | ||
5 5 | ||
\ \ | ||
2 1 | ||
/ | ||
-1 | ||
|
||
*This question was asked by Apple.* |
15 changes: 15 additions & 0 deletions
15
...ingProblem/tests/DailyCodingProblem.Challenges.UnitTests/Y2024/M11/Day30/SolutionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using DailyCodingProblem.Challenges.Y2024.M11.Day30; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DailyCodingProblem.Challenges.UnitTests.Y2024.M11.Day30; | ||
|
||
public class SolutionTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData))] | ||
public void GivenTreeNode_WhenFindMinPathSum_ThenResultAsExpected(TreeNode root, int expectedResult) | ||
{ | ||
Solution.FindMinPathSum(root).ShouldBe(expectedResult); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
DailyCodingProblem/tests/DailyCodingProblem.Challenges.UnitTests/Y2024/M11/Day30/TestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System.Collections; | ||
using DailyCodingProblem.Challenges.Y2024.M11.Day30; | ||
|
||
namespace DailyCodingProblem.Challenges.UnitTests.Y2024.M11.Day30; | ||
|
||
public class TestData : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return [null!, 0]; | ||
|
||
yield return [new TreeNode(), 0]; | ||
|
||
yield return [new TreeNode { Value = int.MaxValue }, int.MaxValue]; | ||
|
||
yield return [new TreeNode { Value = int.MinValue }, int.MinValue]; | ||
|
||
// The test tree: | ||
// 10 | ||
// / \ | ||
// 5 5 | ||
// \ \ | ||
// 2 1 | ||
// / | ||
// -1 | ||
|
||
var root = new TreeNode | ||
{ | ||
Value = 10, | ||
Left = new TreeNode | ||
{ | ||
Value = 5, | ||
Right = new TreeNode { Value = 2 } | ||
}, | ||
Right = new TreeNode | ||
{ | ||
Value = 5, | ||
Right = new TreeNode | ||
{ | ||
Value = 1, | ||
Left = new TreeNode { Value = -1 } | ||
} | ||
} | ||
}; | ||
|
||
yield return [root, 15]; | ||
|
||
// The test tree: | ||
// 5 | ||
// / \ | ||
// 3 2 | ||
// \ | ||
// 2 | ||
|
||
root = new TreeNode | ||
{ | ||
Value = 5, | ||
Left = new TreeNode | ||
{ | ||
Value = 3, | ||
Right = new TreeNode { Value = 2 } | ||
}, | ||
Right = new TreeNode { Value = 2 } | ||
}; | ||
|
||
yield return [root, 7]; | ||
|
||
// The test tree: | ||
// 10 | ||
// / \ | ||
// 20 20 | ||
// / \ / \ | ||
// 30 30 30 30 | ||
|
||
root = new TreeNode | ||
{ | ||
Value = 10, | ||
Left = new TreeNode | ||
{ | ||
Value = 20, | ||
Left = new TreeNode { Value = 30 }, | ||
Right = new TreeNode { Value = 30 } | ||
}, | ||
Right = new TreeNode | ||
{ | ||
Value = 20, | ||
Left = new TreeNode { Value = 30 }, | ||
Right = new TreeNode { Value = 30 } | ||
} | ||
}; | ||
|
||
yield return [root, 60]; | ||
|
||
// The test tree: | ||
// 1 | ||
// / \ | ||
// 2 2 | ||
// / \ / \ | ||
// 4 3 3 4 | ||
// \ / | ||
// -2 -3 | ||
|
||
root = new TreeNode | ||
{ | ||
Value = 1, | ||
Left = new TreeNode | ||
{ | ||
Value = 2, | ||
Left = new TreeNode | ||
{ | ||
Value = 4, | ||
Right = new TreeNode { Value = -2 } | ||
}, | ||
Right = new TreeNode { Value = 3 } | ||
}, | ||
Right = new TreeNode | ||
{ | ||
Value = 2, | ||
Left = new TreeNode { Value = 3 }, | ||
Right = new TreeNode | ||
{ | ||
Value = 4, | ||
Left = new TreeNode { Value = -3 } | ||
} | ||
} | ||
}; | ||
|
||
yield return [root, 4]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} |