-
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 #117 [Easy]. 2024-11-12. Minimum Level Sum of a …
…Binary Tree.
- Loading branch information
1 parent
a3a4d89
commit 1199531
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
DailyCodingProblem/src/DailyCodingProblem.Challenges/Y2024/M11/Day12/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,44 @@ | ||
namespace DailyCodingProblem.Challenges.Y2024.M11.Day12; | ||
|
||
// Time Complexity: O(n), where 'n' is the number of nodes. | ||
// Space Complexity: O(w), where 'w' is the maximum width of the tree. | ||
// It is typically proportional to the number of nodes in the largest level. | ||
public static class Solution | ||
{ | ||
public static int GetTreeLevelWithMinSum(TreeNode? root) | ||
{ | ||
if (root == null) return -1; | ||
|
||
var queue = new Queue<TreeNode>(); | ||
queue.Enqueue(root); | ||
|
||
var resultDepth = -1; | ||
var minSum = int.MaxValue; | ||
var depth = 0; | ||
|
||
while (queue.Count > 0) | ||
{ | ||
var levelSize = queue.Count; | ||
var currentSum = 0; | ||
|
||
for (var i = 0; i < levelSize; i++) | ||
{ | ||
var node = queue.Dequeue(); | ||
currentSum += node.Value; | ||
|
||
if (node.Left != null) queue.Enqueue(node.Left); | ||
if (node.Right != null) queue.Enqueue(node.Right); | ||
} | ||
|
||
if (currentSum < minSum) | ||
{ | ||
minSum = currentSum; | ||
resultDepth = depth; | ||
} | ||
|
||
depth++; | ||
} | ||
|
||
return resultDepth; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
DailyCodingProblem/src/DailyCodingProblem.Challenges/Y2024/M11/Day12/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,8 @@ | ||
namespace DailyCodingProblem.Challenges.Y2024.M11.Day12; | ||
|
||
public class TreeNode(int value) | ||
{ | ||
public int Value { get; set; } = value; | ||
public TreeNode? Left { get; init; } | ||
public TreeNode? Right { get; init; } | ||
} |
111 changes: 111 additions & 0 deletions
111
...ingProblem/tests/DailyCodingProblem.Challenges.UnitTests/Y2024/M11/Day12/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,111 @@ | ||
using DailyCodingProblem.Challenges.Y2024.M11.Day12; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DailyCodingProblem.Challenges.UnitTests.Y2024.M11.Day12; | ||
|
||
public class SolutionTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(TestData))] | ||
public void GivenTree_WhenGetTreeLevelWithMinSum_ThenResultAsExpected(TreeNode root, int expected) | ||
{ | ||
Solution.GetTreeLevelWithMinSum(root).ShouldBe(expected); | ||
} | ||
|
||
public static IEnumerable<object[]> TestData() | ||
{ | ||
var root = new TreeNode(9) | ||
{ | ||
Left = new TreeNode(5) | ||
{ | ||
Left = new TreeNode(1), | ||
Right = new TreeNode(2), | ||
}, | ||
Right = new TreeNode(10) | ||
{ | ||
Left = new TreeNode(3) | ||
} | ||
}; | ||
yield return [root, 2]; | ||
|
||
yield return [new TreeNode(5), 0]; | ||
|
||
root = new TreeNode(3) | ||
{ | ||
Left = new TreeNode(-1), | ||
Right = new TreeNode(-2) | ||
}; | ||
yield return [root, 1]; | ||
|
||
root = new TreeNode(1) | ||
{ | ||
Left = new TreeNode(2) | ||
{ | ||
Left = new TreeNode(4), | ||
Right = new TreeNode(5), | ||
}, | ||
Right = new TreeNode(3) | ||
{ | ||
Left = new TreeNode(6), | ||
Right = new TreeNode(7), | ||
} | ||
}; | ||
yield return [root, 0]; | ||
|
||
root = new TreeNode(2) | ||
{ | ||
Left = new TreeNode(2), | ||
Right = new TreeNode(2) | ||
{ | ||
Left = new TreeNode(2), | ||
Right = new TreeNode(2), | ||
} | ||
}; | ||
yield return [root, 0]; | ||
|
||
root = new TreeNode(1) | ||
{ | ||
Left = new TreeNode(7) | ||
{ | ||
Left = new TreeNode(0), | ||
Right = new TreeNode(-4) | ||
}, | ||
Right = new TreeNode(-3) | ||
}; | ||
yield return [root, 2]; | ||
|
||
root = new TreeNode(10) | ||
{ | ||
Left = new TreeNode(5) | ||
{ | ||
Left = new TreeNode(4) | ||
{ | ||
Left = new TreeNode(1) | ||
{ | ||
Left = new TreeNode(-6) | ||
} | ||
} | ||
} | ||
}; | ||
yield return [root, 4]; | ||
|
||
// -5 | ||
// -10 -15 | ||
// -20 -30 -25 -35 | ||
root = new TreeNode(-5) | ||
{ | ||
Left = new TreeNode(-10) | ||
{ | ||
Left = new TreeNode(-20), | ||
Right = new TreeNode(-30) | ||
}, | ||
Right = new TreeNode(-15) | ||
{ | ||
Left = new TreeNode(-25), | ||
Right = new TreeNode(-35) | ||
} | ||
}; | ||
yield return [root, 2]; | ||
} | ||
} |