Skip to content

Commit

Permalink
Daily Coding Problem #135 [Easy]. 2024-11-30.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Dec 9, 2024
1 parent 61ba239 commit 1b3a17b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
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;
}
}
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; }
}
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.*
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);
}
}
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();
}

0 comments on commit 1b3a17b

Please sign in to comment.