-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from vaskoz/day94
Day94
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
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
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,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 | ||
} |
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,35 @@ | ||
package day94 | ||
|
||
import "testing" | ||
|
||
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) | ||
} | ||
} | ||
|
||
func BenchmarkMaxPathSum(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
MaxPathSum(tree) | ||
} | ||
} |