Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 237
Good morning! Here's your coding interview problem for today.

This problem was asked by Amazon.

A tree is symmetric if its data and shape remain unchanged when it is reflected about the root node. The following tree is an example:
```
        4
      / | \
    3   5   3
  /           \
9              9
```
Given a `k-ary` tree, determine whether it is symmetric.

Pull request description:
Day237
Resolves #487.

Patch:
diff --git a/README.md b/README.md
index 4c80f8b..09b2c82 100644
--- a/README.md
+++ b/README.md
@@ -245,3 +245,4 @@ problems from
 * [Day 234](https://github.com/vaskoz/dailycodingproblem-go/issues/481)
 * [Day 235](https://github.com/vaskoz/dailycodingproblem-go/issues/483)
 * [Day 236](https://github.com/vaskoz/dailycodingproblem-go/issues/485)
+* [Day 237](https://github.com/vaskoz/dailycodingproblem-go/issues/487)
diff --git a/day237/problem.go b/day237/problem.go
new file mode 100644
index 0000000..7ce84d6
--- /dev/null
+++ b/day237/problem.go
@@ -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
+}


Test patch:
diff --git a/day237/problem_test.go b/day237/problem_test.go
new file mode 100644
index 0000000..ab3aedd
--- /dev/null
+++ b/day237/problem_test.go
@@ -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)
+		}
+	}
+}