Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day117 #274

Merged
merged 2 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ problems from
* [Day 114](https://github.com/vaskoz/dailycodingproblem-go/issues/237)
* [Day 115](https://github.com/vaskoz/dailycodingproblem-go/issues/238)
* [Day 116](https://github.com/vaskoz/dailycodingproblem-go/issues/242)
* [Day 117](https://github.com/vaskoz/dailycodingproblem-go/issues/245)
* [Day 118](https://github.com/vaskoz/dailycodingproblem-go/issues/246)
* [Day 120](https://github.com/vaskoz/dailycodingproblem-go/issues/248)
* [Day 122](https://github.com/vaskoz/dailycodingproblem-go/issues/252)
Expand Down
41 changes: 41 additions & 0 deletions day117/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package day117

// BinaryTree is a binary tree of integers.
type BinaryTree struct {
Value int
Left, Right *BinaryTree
}

// MinimumSumLevel returns both the 1-based level and sum of that level.
// Runs in O(N) time and O(1) extra space.
// If you pass in "nil", the sum is 0 and the level is 0.
func MinimumSumLevel(tree *BinaryTree) (minSum int, minLevel int) {
if tree == nil {
return
}
minSum = int(^uint(0) >> 1)
minLevel = minSum
current := make([]*BinaryTree, 0, 1)
current = append(current, tree)
level := 1
for len(current) != 0 {
nextLevel := make([]*BinaryTree, 0, 2*cap(current))
var sum int
for _, ptr := range current {
sum += ptr.Value
if ptr.Left != nil {
nextLevel = append(nextLevel, ptr.Left)
}
if ptr.Right != nil {
nextLevel = append(nextLevel, ptr.Right)
}
}
if sum < minSum {
minSum = sum
minLevel = level
}
level++
current = nextLevel
}
return
}
65 changes: 65 additions & 0 deletions day117/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package day117

import "testing"

var testcases = []struct {
tree *BinaryTree
minLevel, minSum int
}{
{nil, 0, 0},
{&BinaryTree{5,
&BinaryTree{10, nil, nil},
&BinaryTree{20, nil, nil}},
1,
5},
{&BinaryTree{5,
&BinaryTree{10, nil, nil},
&BinaryTree{-20, nil, nil}},
2,
-10},
{&BinaryTree{5,
&BinaryTree{10,
&BinaryTree{30,
nil,
&BinaryTree{0,
&BinaryTree{-7, nil, nil},
&BinaryTree{-9, nil, nil}}},
nil},
&BinaryTree{20,
nil,
&BinaryTree{0,
&BinaryTree{3, nil, nil},
&BinaryTree{5, nil, nil}}}},
5,
-16},
{&BinaryTree{5,
&BinaryTree{10,
&BinaryTree{30,
nil,
&BinaryTree{-9, nil, nil}},
nil},
&BinaryTree{20,
nil,
&BinaryTree{0,
&BinaryTree{3, nil, nil},
&BinaryTree{5, nil, nil}}}},
4,
-1},
}

func TestMinimumSumLevel(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if sum, level := MinimumSumLevel(tc.tree); sum != tc.minSum && level != tc.minLevel {
t.Errorf("Expected (%d,%d), but got (%d,%d)", tc.minSum, tc.minLevel, sum, level)
}
}
}

func BenchmarkMinimumSumLevel(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MinimumSumLevel(tc.tree)
}
}
}