-
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
93 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,31 @@ | ||
// Package day237 has a note. | ||
// NOTE: I didn't provide an API for a k-ary | ||
// tree. It's up to the user to build a valid | ||
// k-ary tree. | ||
package day237 | ||
|
||
// KaryTree is a k-ary tree of integers. | ||
type KaryTree struct { | ||
Value int | ||
Children []*KaryTree | ||
} | ||
|
||
// IsTreeSymmetric answers if its data and shape remain | ||
// unchanged when it is reflected about the root node. | ||
func IsTreeSymmetric(tree *KaryTree) bool { | ||
return equal(tree, tree) | ||
} | ||
|
||
func equal(a, b *KaryTree) bool { | ||
if a == nil && b == nil { | ||
return true | ||
} else if a != nil && b != nil && a.Value == b.Value { | ||
for i := range a.Children { | ||
if !equal(a.Children[i], b.Children[len(a.Children)-1-i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} |
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,61 @@ | ||
package day237 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
root *KaryTree | ||
symmetric bool | ||
}{ | ||
{&KaryTree{ | ||
4, | ||
[]*KaryTree{ | ||
{ | ||
3, | ||
[]*KaryTree{{9, nil}, nil, nil}, | ||
}, | ||
{ | ||
5, | ||
[]*KaryTree{nil, nil, nil}, | ||
}, | ||
{ | ||
3, | ||
[]*KaryTree{nil, nil, {9, nil}}, | ||
}, | ||
}, | ||
}, true}, | ||
{nil, true}, | ||
{&KaryTree{ | ||
4, | ||
[]*KaryTree{ | ||
{ | ||
3, | ||
[]*KaryTree{nil, {9, nil}, nil}, | ||
}, | ||
{ | ||
5, | ||
[]*KaryTree{nil, nil, nil}, | ||
}, | ||
{ | ||
3, | ||
[]*KaryTree{nil, nil, {9, nil}}, | ||
}, | ||
}, | ||
}, false}, | ||
} | ||
|
||
func TestIsTreeSymmetric(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := IsTreeSymmetric(tc.root); result != tc.symmetric { | ||
t.Errorf("Expected %v, got %v", tc.symmetric, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIsTreeSymmetric(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
IsTreeSymmetric(tc.root) | ||
} | ||
} | ||
} |