From 84b5a0d759fdd8f8f151397c8650ba121d5780e1 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Tue, 1 Jan 2019 23:09:38 -0700 Subject: [PATCH 1/2] day 117: return minLevel and minSum of a binary tree --- day117/problem.go | 41 ++++++++++++++++++++++++++ day117/problem_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 day117/problem.go create mode 100644 day117/problem_test.go diff --git a/day117/problem.go b/day117/problem.go new file mode 100644 index 0000000..f284fd3 --- /dev/null +++ b/day117/problem.go @@ -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 +} diff --git a/day117/problem_test.go b/day117/problem_test.go new file mode 100644 index 0000000..f5aeff1 --- /dev/null +++ b/day117/problem_test.go @@ -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) + } + } +} From 946f43d197c14c563736360e145a31c0f00fedb3 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Tue, 1 Jan 2019 23:10:30 -0700 Subject: [PATCH 2/2] add day 117 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 83436b0..6ddb8cd 100644 --- a/README.md +++ b/README.md @@ -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)