Skip to content

Commit

Permalink
Merge 3adb9b4 into f5d0a23
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jan 17, 2020
2 parents f5d0a23 + 3adb9b4 commit 61f4c9e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,5 @@ problems from
* [Day 419](https://github.com/vaskoz/dailycodingproblem-go/issues/846)
* [Day 420](https://github.com/vaskoz/dailycodingproblem-go/issues/848)
* [Day 421](https://github.com/vaskoz/dailycodingproblem-go/issues/850)
* [Day 422](https://github.com/vaskoz/dailycodingproblem-go/issues/852)

33 changes: 33 additions & 0 deletions day422/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day422

// BinaryTree is an integer binary tree.
type BinaryTree struct {
Val int
Left, Right *BinaryTree
}

// MergeBinaryTrees returns a new binary tree that is the "merge"
// of two other binary trees.
// Merge is defined as the sum of equivalently placed nodes.
// Runs in O(N) time.
func MergeBinaryTrees(one, two *BinaryTree) *BinaryTree {
switch {
case one == nil && two == nil:
return nil
case one == nil:
return &BinaryTree{two.Val,
MergeBinaryTrees(nil, two.Left),
MergeBinaryTrees(nil, two.Right),
}
case two == nil:
return &BinaryTree{one.Val,
MergeBinaryTrees(one.Left, nil),
MergeBinaryTrees(one.Right, nil),
}
default:
return &BinaryTree{one.Val + two.Val,
MergeBinaryTrees(one.Left, two.Left),
MergeBinaryTrees(one.Right, two.Right),
}
}
}
113 changes: 113 additions & 0 deletions day422/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package day422

import "testing"

// nolint
var testcases = []struct {
one, two, merged *BinaryTree
}{
{
&BinaryTree{
5,
&BinaryTree{
Val: 9,
},
&BinaryTree{
Val: 11,
},
},
&BinaryTree{
6,
&BinaryTree{
Val: 10,
},
&BinaryTree{
Val: 21,
},
},
&BinaryTree{
11,
&BinaryTree{
Val: 19,
},
&BinaryTree{
Val: 32,
},
},
},
{
&BinaryTree{
5,
&BinaryTree{
Val: 9,
},
nil,
},
&BinaryTree{
6,
nil,
&BinaryTree{
Val: 21,
},
},
&BinaryTree{
11,
&BinaryTree{
Val: 9,
},
&BinaryTree{
Val: 21,
},
},
},
{
&BinaryTree{
5,
&BinaryTree{
Val: 9,
},
nil,
},
nil,
&BinaryTree{
5,
&BinaryTree{
Val: 9,
},
nil,
},
},
}

func TestMergeBinaryTree(t *testing.T) {
t.Parallel()

for tcid, tc := range testcases {
if result := MergeBinaryTrees(tc.one, tc.two); !equal(result, tc.merged) {
t.Errorf("Trees in TCID%d don't match", tcid)
}
}
}

func BenchmarkMergeBinaryTree(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
MergeBinaryTrees(tc.one, tc.two)
}
}
}

func equal(a, b *BinaryTree) bool {
switch {
case a == nil && b != nil:
return false
case a != nil && b == nil:
return false
case a == nil && b == nil:
return true
case a.Val != b.Val:
return false
}

return equal(a.Left, b.Left) && equal(a.Right, b.Right)
}

0 comments on commit 61f4c9e

Please sign in to comment.