-
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.
- Loading branch information
Showing
3 changed files
with
147 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,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), | ||
} | ||
} | ||
} |
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,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) | ||
} |