From 7441c48a3fc1f4e5dbeb10c89fc7e7c616ecf433 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Mon, 26 Nov 2018 18:30:08 -0600 Subject: [PATCH 1/3] day 94: maximum path sum in binary tree --- day94/problem.go | 41 ++++++++++++++++++++++++++++++++++ day94/problem_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 day94/problem.go create mode 100644 day94/problem_test.go diff --git a/day94/problem.go b/day94/problem.go new file mode 100644 index 0000000..b70eb90 --- /dev/null +++ b/day94/problem.go @@ -0,0 +1,41 @@ +package day94 + +// BinaryTree is a binary tree with an integer value. +type BinaryTree struct { + Value int + Left, Right *BinaryTree +} + +// MaxPathSum returns the maximum path sum in a given +// binary tree. +func MaxPathSum(tree *BinaryTree) int { + const MinInt = -int(^uint(0)>>1) - 1 + result := MinInt + maxPathSum(tree, &result) + return result +} + +func maxPathSum(node *BinaryTree, res *int) int { + if node == nil { + return 0 + } else if node.Left == nil && node.Right == nil { + return node.Value + } + leftSum := maxPathSum(node.Left, res) + rightSum := maxPathSum(node.Right, res) + if node.Left != nil && node.Right != nil { + *res = max(*res, leftSum+rightSum+node.Value) + return max(leftSum, rightSum) + node.Value + } + if node.Left == nil { + return rightSum + node.Value + } + return leftSum + node.Value +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} diff --git a/day94/problem_test.go b/day94/problem_test.go new file mode 100644 index 0000000..cf2da53 --- /dev/null +++ b/day94/problem_test.go @@ -0,0 +1,51 @@ +package day94 + +import "testing" + +func TestMaxPathSum(t *testing.T) { + t.Parallel() + tree := &BinaryTree{ + -15, + &BinaryTree{5, + &BinaryTree{-8, + &BinaryTree{2, nil, nil}, + &BinaryTree{6, nil, nil}}, + &BinaryTree{1, nil, nil}}, + &BinaryTree{6, + &BinaryTree{3, nil, nil}, + &BinaryTree{9, nil, + &BinaryTree{0, + &BinaryTree{4, nil, nil}, + &BinaryTree{-1, &BinaryTree{10, nil, nil}, nil}, + }, + }, + }, + } + result := MaxPathSum(tree) + if result != 27 { + t.Errorf("Expected 27 got %v", result) + } +} + +func BenchmarkMaxPathSum(b *testing.B) { + tree := &BinaryTree{ + -15, + &BinaryTree{5, + &BinaryTree{-8, + &BinaryTree{2, nil, nil}, + &BinaryTree{6, nil, nil}}, + &BinaryTree{1, nil, nil}}, + &BinaryTree{6, + &BinaryTree{3, nil, nil}, + &BinaryTree{9, nil, + &BinaryTree{0, + &BinaryTree{4, nil, nil}, + &BinaryTree{-1, &BinaryTree{10, nil, nil}, nil}, + }, + }, + }, + } + for i := 0; i < b.N; i++ { + MaxPathSum(tree) + } +} From 7de8f90506c31b552d44c77de8e7584ee0f79285 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Mon, 26 Nov 2018 18:30:54 -0600 Subject: [PATCH 2/3] add day 94 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 27f2276..38172cc 100644 --- a/README.md +++ b/README.md @@ -102,5 +102,6 @@ problems from * [Day 90](https://github.com/vaskoz/dailycodingproblem-go/issues/189) * [Day 91](https://github.com/vaskoz/dailycodingproblem-go/issues/192) * [Day 92](https://github.com/vaskoz/dailycodingproblem-go/issues/194) +* [Day 94](https://github.com/vaskoz/dailycodingproblem-go/issues/197) * [Day 95](https://github.com/vaskoz/dailycodingproblem-go/issues/198) * [Day 96](https://github.com/vaskoz/dailycodingproblem-go/issues/200) From a2a27f5690006aac2f13fc0c46318efd80995c4f Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Mon, 26 Nov 2018 20:26:19 -0700 Subject: [PATCH 3/3] day 94: DRY the creation of the test tree --- day94/problem_test.go | 52 +++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/day94/problem_test.go b/day94/problem_test.go index cf2da53..de2caa1 100644 --- a/day94/problem_test.go +++ b/day94/problem_test.go @@ -2,25 +2,26 @@ package day94 import "testing" -func TestMaxPathSum(t *testing.T) { - t.Parallel() - tree := &BinaryTree{ - -15, - &BinaryTree{5, - &BinaryTree{-8, - &BinaryTree{2, nil, nil}, - &BinaryTree{6, nil, nil}}, - &BinaryTree{1, nil, nil}}, - &BinaryTree{6, - &BinaryTree{3, nil, nil}, - &BinaryTree{9, nil, - &BinaryTree{0, - &BinaryTree{4, nil, nil}, - &BinaryTree{-1, &BinaryTree{10, nil, nil}, nil}, - }, +var tree = &BinaryTree{ + -15, + &BinaryTree{5, + &BinaryTree{-8, + &BinaryTree{2, nil, nil}, + &BinaryTree{6, nil, nil}}, + &BinaryTree{1, nil, nil}}, + &BinaryTree{6, + &BinaryTree{3, nil, nil}, + &BinaryTree{9, nil, + &BinaryTree{0, + &BinaryTree{4, nil, nil}, + &BinaryTree{-1, &BinaryTree{10, nil, nil}, nil}, }, }, - } + }, +} + +func TestMaxPathSum(t *testing.T) { + t.Parallel() result := MaxPathSum(tree) if result != 27 { t.Errorf("Expected 27 got %v", result) @@ -28,23 +29,6 @@ func TestMaxPathSum(t *testing.T) { } func BenchmarkMaxPathSum(b *testing.B) { - tree := &BinaryTree{ - -15, - &BinaryTree{5, - &BinaryTree{-8, - &BinaryTree{2, nil, nil}, - &BinaryTree{6, nil, nil}}, - &BinaryTree{1, nil, nil}}, - &BinaryTree{6, - &BinaryTree{3, nil, nil}, - &BinaryTree{9, nil, - &BinaryTree{0, - &BinaryTree{4, nil, nil}, - &BinaryTree{-1, &BinaryTree{10, nil, nil}, nil}, - }, - }, - }, - } for i := 0; i < b.N; i++ { MaxPathSum(tree) }