Skip to content

Commit

Permalink
Daily Coding Problem #117 [Easy]. 2024-11-12. Minimum Level Sum of a …
Browse files Browse the repository at this point in the history
…Binary Tree.
  • Loading branch information
eminencegrs committed Nov 12, 2024
1 parent a3a4d89 commit 1199531
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
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;
}
}
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; }
}
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];
}
}

0 comments on commit 1199531

Please sign in to comment.